A [custom bidding algorithm](https://developers.google.com/display-video/api/reference/rest/current/customBiddingAlgorithms#CustomBiddingAlgorithm) is built from bidding logic defined by
the user. Assign it to a line item through their bid strategy. When bidding on
ad inventory, the line item will then use that custom logic.

First, choose an [algorithm type](https://developers.google.com/display-video/api/reference/rest/current/customBiddingAlgorithms#custombiddingalgorithmtype) and create the [algorithm
resource](https://developers.google.com/display-video/api/reference/rest/current/customBiddingAlgorithms#CustomBiddingAlgorithm).

## Choose a Custom Bidding Algorithm type

The [algorithm type](https://developers.google.com/display-video/api/reference/rest/current/customBiddingAlgorithms#custombiddingalgorithmtype) dictates how the bidding logic is defined in the
algorithm.

Choose one of the following types that can be created through the Display \& Video 360 API:

- `SCRIPT_BASED`: Logic set by an uploaded [script](https://support.google.com/displayvideo/answer/9728993). The script is a text file that uses basic Python syntax.
- `RULE_BASED`: Logic set by an uploaded [ruleset](https://support.google.com/displayvideo/answer/11118987). The ruleset is an uploaded [`AlgorithmRules`](https://developers.google.com/display-video/api/reference/rest/current/AlgorithmRules) JSON file.

## Create [`CustomBiddingAlgorithm`](https://developers.google.com/display-video/api/reference/rest/current/customBiddingAlgorithms#CustomBiddingAlgorithm) resource

Use a [`create`](https://developers.google.com/display-video/api/reference/rest/current/customBiddingAlgorithms/create) request to make a custom bidding algorithm.

Either an advertiser or partner can be the [`owner`](https://developers.google.com/display-video/api/reference/rest/current/customBiddingAlgorithms#CustomBiddingAlgorithm.FIELDS.oneof_owner) of an
algorithm. If owned by an advertiser, it can only be used by ad campaigns under
that advertiser. If owned by a partner, it can only be used by advertisers with
which it is [intentionally shared](https://developers.google.com/display-video/api/reference/rest/current/customBiddingAlgorithms#CustomBiddingAlgorithm.FIELDS.shared_advertiser_ids).

> [!WARNING]
> **Warning:** If a `RULE_BASED` algorithm is owned by a partner, it can only be [shared](https://developers.google.com/display-video/api/reference/rest/current/customBiddingAlgorithms#CustomBiddingAlgorithm.FIELDS.shared_advertiser_ids) by a single advertiser.

Here's how to create a custom bidding algorithm:

### Java

```java
// Provide the ID of the advertiser that will own the algorithm.
long advertiserId = advertiser-id;

// Provide the display name of the algorithm.
String displayName = display-name;

// Provide the type of custom bidding algorithm.
String customBiddingAlgorithmType = custom-bidding-algorithm-type;

// Create the custom bidding algorithm structure.
CustomBiddingAlgorithm algorithm =
    new CustomBiddingAlgorithm()
        .setAdvertiserId(advertiserId)
        .setDisplayName(displayName)
        .setEntityStatus("ENTITY_STATUS_ACTIVE")
        .setCustomBiddingAlgorithmType(customBiddingAlgorithmType);

// Create the algorithm.
CustomBiddingAlgorithm response = service.customBiddingAlgorithms().create(algorithm).execute();

// Display the new custom bidding algorithm ID.
System.out.printf(
    "Custom Bidding Algorithm was created with the ID %s.",
    response.getCustomBiddingAlgorithmId());
```

### Python

```python
# Provide the ID of the advertiser that will own the algorithm.
advertiser_id = advertiser-id

# Provide the display name of the algorithm.
display_name = display-name

# Provide the type of custom bidding algorithm.
custom_bidding_algo_type = custom-bidding-algorithm-type

# Build CustomBiddingAlgorithm object.
custom_bidding_algorithm_obj = {
    "advertiserId": advertiser_id,
    "displayName": display_name,
    "entityStatus": "ENTITY_STATUS_ACTIVE",
    "customBiddingAlgorithmType": custom_bidding_algo_type,
}

# Build and execute request.
algorithm_response = (
    service.customBiddingAlgorithms()
    .create(body=custom_bidding_algorithm_obj)
    .execute()
)

# Print ID of new custom bidding algorithm.
print(
    "Custom bidding algorithm was created with ID "
    f'{algorithm_response["customBiddingAlgorithmId"]}.'
)
```

### PHP

```php
// Provide the ID of the advertiser that will own the algorithm.
$advertiserId = advertiser-id;

// Provide the display name of the algorithm.
$displayName = display-name;

// Provide the type of custom bidding algorithm.
$customBiddingAlgorithmType = custom-bidding-algorithm-type;

// Create the custom bidding algorithm structure.
$algorithm = new Google_Service_DisplayVideo_CustomBiddingAlgorithm();
$algorithm->setAdvertiserId($advertiserId);
$algorithm->setDisplayName($displayName);
$algorithm->setEntityStatus('ENTITY_STATUS_ACTIVE');
$algorithm->setCustomBiddingAlgorithmType($customBiddingAlgorithmType);

// Call the API, creating the advertiser.
try {
    $result = $this->service->customBiddingAlgorithms->create($algorithm);
} catch (\Exception $e) {
    $this->renderError($e);
    return;
}

// Print ID of new custom bidding algorithm.
printf('<p>Custom Bidding Algorithm was created with the ID %s.</p>', $result['customBiddingAlgorithmId']);
```