A budget can be applied to a single campaign, or be
[shared](https://support.google.com/google-ads/answer/2517512) across multiple
campaigns.

## Setup

When you [create](https://developers.google.com/google-ads/api/docs/campaigns/budgets/create-budgets.md.txt) a new
[`CampaignBudget`](https://developers.google.com/google-ads/api/reference/rpc/v26/CampaignBudget.md.txt) using the Google Ads API, you can
specify whether it is shareable using the boolean
[`explicitly_shared`](https://developers.google.com/google-ads/api/reference/rpc/v26/CampaignBudget.md.txt#explicitly_shared)
field:

- `true` (default): Can be shared among multiple campaigns.
- `false`: Can be used by only one campaign.

> [!IMPORTANT]
> **Key Point:** Once the budget is created, you cannot change `explicitly_shared` from `true` to `false`. However, you can change `explicitly_shared` from `false` to `true`, provided there are no [experiments](https://support.google.com/google-ads/answer/2385204) running on the campaign that's using the budget.

Explicitly shared budgets appear in an account's **Shared Library** in the
Google Ads UI, whereas a non-shared budget appears only within its associated
campaign's **Settings**.

In the following example where a new budget is created, the budget is shared
because `explicitly_shared` is set to `true`:


### Java

```java
private String createSharedCampaignBudget(GoogleAdsClient googleAdsClient, long customerId) {
  try (CampaignBudgetServiceClient campaignBudgetServiceClient =
      googleAdsClient.getLatestVersion().createCampaignBudgetServiceClient()) {
    // Creates a shared budget.
    CampaignBudget budget =
        CampaignBudget.newBuilder()
            .setName("Shared Interplanetary Budget #" + getPrintableDateTime())
            .setAmountMicros(50_000_000L)
            .setDeliveryMethod(BudgetDeliveryMethod.STANDARD)
            .setExplicitlyShared(true)
            .build();
    // Constructs an operation that will create a shared budget.
    CampaignBudgetOperation operation =
        CampaignBudgetOperation.newBuilder().setCreate(budget).build();
    // Sends the operation in a mutate request.
    MutateCampaignBudgetsResponse response =
        campaignBudgetServiceClient.mutateCampaignBudgets(
            Long.toString(customerId), Lists.newArrayList(operation));

    MutateCampaignBudgetResult mutateCampaignBudgetResult = response.getResults(0);
    // Prints the resource name of the created object.
    System.out.printf(
        "Created shared budget with resource name: '%s'.%n",
        mutateCampaignBudgetResult.getResourceName());

    return mutateCampaignBudgetResult.getResourceName();
  }
}
      
```

### C#

```c#
private string CreateSharedBudget(GoogleAdsClient client, long customerId, string name,
    long amount)
{
    // Get the CampaignBudgetService.
    CampaignBudgetServiceClient campaignBudgetService =
        client.GetService(Services.V26.CampaignBudgetService);

    // Create a shared budget.
    CampaignBudget budget = new CampaignBudget()
    {
        Name = name,
        AmountMicros = amount,
        DeliveryMethod = BudgetDeliveryMethodEnum.Types.BudgetDeliveryMethod.Standard,
        ExplicitlyShared = true
    };

    // Create the operation.
    CampaignBudgetOperation campaignBudgetOperation = new CampaignBudgetOperation()
    {
        Create = budget
    };

    // Make the mutate request.
    MutateCampaignBudgetsResponse retVal = campaignBudgetService.MutateCampaignBudgets(
        customerId.ToString(), new CampaignBudgetOperation[] { campaignBudgetOperation });
    return retVal.Results[0].ResourceName;
}
      
```

### PHP

```php
    private static function createSharedCampaignBudget(
        GoogleAdsClient $googleAdsClient,
        int $customerId
    ) {
        // Creates a shared budget.
        $budget = new CampaignBudget(
            [
            'name' => 'Shared Interplanetary Budget #' . Helper::getPrintableDatetime(),
            'delivery_method' => BudgetDeliveryMethod::STANDARD,
            // Sets the amount of budget.
            'amount_micros' => 50000000,
            // Makes the budget explicitly shared.
            'explicitly_shared' => true
            ]
        );

        // Constructs a campaign budget operation.
        $campaignBudgetOperation = new CampaignBudgetOperation();
        $campaignBudgetOperation->setCreate($budget);

        // Issues a mutate request to create the budget.
        $campaignBudgetServiceClient = $googleAdsClient->getCampaignBudgetServiceClient();
        $response = $campaignBudgetServiceClient->mutateCampaignBudgets(
            MutateCampaignBudgetsRequest::build($customerId, [$campaignBudgetOperation])
        );

        /**
 * @var CampaignBudget $addedBudget
*/
        $addedBudget = $response->getResults()[0];
        printf(
            "Created a shared budget with resource name '%s'.%s",
            $addedBudget->getResourceName(),
            PHP_EOL
        );

        return $addedBudget->getResourceName();
    }
      
```

### Python

```python
# Create a budget, which can be shared by multiple campaigns.
campaign_budget_operation: CampaignBudgetOperation = client.get_type(
    "CampaignBudgetOperation"
)
campaign_budget: CampaignBudget = campaign_budget_operation.create
campaign_budget.name = f"Interplanetary Budget {uuid.uuid4()}"
campaign_budget.delivery_method = (
    client.enums.BudgetDeliveryMethodEnum.STANDARD
)
campaign_budget.amount_micros = 500000
campaign_budget.explicitly_shared = True

# Add budget.
try:
    campaign_budget_response: MutateCampaignBudgetsResponse = (
        campaign_budget_service.mutate_campaign_budgets(
            customer_id=customer_id, operations=[campaign_budget_operation]
        )
    )
    campaign_budget_id: str = campaign_budget_response.results[
        0
    ].resource_name
    print(f'Budget "{campaign_budget_id}" was created.')
except GoogleAdsException as ex:
    handle_googleads_exception(ex)
      
```

### Ruby

```ruby
# Create a budget, which can be shared by multiple campaigns.
budget = client.resource.campaign_budget do |cb|
  cb.name = "Interplanetary budget ##{(Time.new.to_f * 1000).to_i}"
  cb.amount_micros = 50_000_000
  cb.delivery_method = :STANDARD
  cb.explicitly_shared = true
end

operation = client.operation.create_resource.campaign_budget(budget)

response = client.service.campaign_budget.mutate_campaign_budgets(
  customer_id: customer_id,
  operations: [operation],
)
budget_id = response.results.first.resource_name
      
```

### Perl

```perl
sub create_shared_campaign_buget {
  my ($api_client, $customer_id) = @_;

  # Create a shared budget.
  my $campaign_budget =
    Google::Ads::GoogleAds::V26::Resources::CampaignBudget->new({
      name           => "Shared Interplanetary Budget #" . uniqid(),
      deliveryMethod => STANDARD,
      # Set the amount of budget.
      amountMicros => 50000000,
      # Makes the budget explicitly shared.
      explicitlyShared => 'true'
    });

  # Create a campaign budget operation.
  my $campaign_budget_operation =
    Google::Ads::GoogleAds::V26::Services::CampaignBudgetService::CampaignBudgetOperation
    ->new({create => $campaign_budget});

  # Add the campaign budget.
  my $campaign_budgets_response = $api_client->CampaignBudgetService()->mutate({
      customerId => $customer_id,
      operations => [$campaign_budget_operation]});

  my $campaign_budget_resource_name =
    $campaign_budgets_response->{results}[0]{resourceName};

  printf "Created a shared budget with resource name: '%s'.\n",
    $campaign_budget_resource_name;

  return $campaign_budget_resource_name;
}
      
```

### curl

> [!NOTE]
> **Note:** While a direct REST code sample for this step isn't provided here, you can achieve this using a manual REST request.   
>
> Refer to the Google Ads API REST interface documentation and the method-specific reference pages. You will need to construct the JSON payload based on the proto definitions.   
>
> Key Resources:
>
> - [Using REST](https://developers.google.com/google-ads/api/rest/overview.md.txt)
> - [REST Interface Structure](https://developers.google.com/google-ads/api/rest/design/overview.md.txt)
> - [JSON Field Mappings](https://developers.google.com/google-ads/api/rest/design/json-mappings.md.txt)
> - Consult the [REST API Reference](https://developers.google.com/google-ads/api/reference/rpc/latest/overview) for the specific service and method.

<br />

## Determine if a campaign budget is shared

You can retrieve the budget setting value by querying the
[`campaign_budget.explicitly_shared`](https://developers.google.com/google-ads/api/fields/v26/campaign_budget.md.txt#campaign_budget.explicitly_shared)
field. Here is the [GAQL](https://developers.google.com/google-ads/api/docs/query/overview.md.txt) query for the field, filtering
on a budget ID:

```sql
SELECT campaign_budget.explicitly_shared
FROM campaign_budget
WHERE campaign_budget.id = campaign_budget_id
```