Google Ads API is returning to beta status. Please read our blog post for more details.

Create a Campaign Budget

Creating a new CampaignBudget using the Google Ads API is straightforward as demonstrated in the following example.

Java

private static String addCampaignBudget(GoogleAdsClient googleAdsClient, long customerId) {
  CampaignBudget budget =
      CampaignBudget.newBuilder()
          .setName(StringValue.of("Interplanetary Cruise Budget #" + System.currentTimeMillis()))
          .setDeliveryMethod(BudgetDeliveryMethod.STANDARD)
          .setAmountMicros(Int64Value.of(500_000))
          .build();

  CampaignBudgetOperation op = CampaignBudgetOperation.newBuilder().setCreate(budget).build();

  try (CampaignBudgetServiceClient campaignBudgetServiceClient =
      googleAdsClient.getLatestVersion().createCampaignBudgetServiceClient()) {
    MutateCampaignBudgetsResponse response =
        campaignBudgetServiceClient.mutateCampaignBudgets(
            Long.toString(customerId), ImmutableList.of(op));
    String budgetResourceName = response.getResults(0).getResourceName();
    System.out.printf("Added budget: %s%n", budgetResourceName);
    return budgetResourceName;
  }
}

C#

private static string CreateBudget(GoogleAdsClient client, long customerId)
{
    // Get the BudgetService.
    CampaignBudgetServiceClient budgetService = client.GetService(
        Services.V2.CampaignBudgetService);

    // Create the campaign budget.
    CampaignBudget budget = new CampaignBudget()
    {
        Name = "Interplanetary Cruise Budget #" + ExampleUtilities.GetRandomString(),
        DeliveryMethod = BudgetDeliveryMethod.Standard,
        AmountMicros = 500000
    };

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

    // Create the campaign budget.
    MutateCampaignBudgetsResponse response = budgetService.MutateCampaignBudgets(
        customerId.ToString(), new CampaignBudgetOperation[] { budgetOperation });
    return response.Results[0].ResourceName;
}

PHP

private static function addCampaignBudget(GoogleAdsClient $googleAdsClient, int $customerId)
{
    // Creates a campaign budget.
    $budget = new CampaignBudget([
        'name' => new StringValue(['value' => 'Interplanetary Cruise Budget #' . uniqid()]),
        'delivery_method' => BudgetDeliveryMethod::STANDARD,
        'amount_micros' => new Int64Value(['value' => 500000])
    ]);

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

    // Issues a mutate request.
    $campaignBudgetServiceClient = $googleAdsClient->getCampaignBudgetServiceClient();
    $response = $campaignBudgetServiceClient->mutateCampaignBudgets(
        $customerId,
        [$campaignBudgetOperation]
    );

    /** @var CampaignBudget $addedBudget */
    $addedBudget = $response->getResults()[0];
    printf("Added budget named '%s'%s", $addedBudget->getResourceName(), PHP_EOL);

    return $addedBudget->getResourceName();
}

Python

# Create a budget, which can be shared by multiple campaigns.
campaign_budget_operation = client.get_type('CampaignBudgetOperation',
                                            version='v2')
campaign_budget = campaign_budget_operation.create
campaign_budget.name.value = 'Interplanetary Budget %s' % uuid.uuid4()
campaign_budget.delivery_method = client.get_type(
    'BudgetDeliveryMethodEnum').STANDARD
campaign_budget.amount_micros.value = 500000

# Add budget.
try:
    campaign_budget_response = (
        campaign_budget_service.mutate_campaign_budgets(
            customer_id, [campaign_budget_operation]))
except google.ads.google_ads.errors.GoogleAdsException as ex:
    print('Request with ID "%s" failed with status "%s" and includes the '
          'following errors:' % (ex.request_id, ex.error.code().name))
    for error in ex.failure.errors:
        print('\tError with message "%s".' % error.message)
        if error.location:
            for field_path_element in error.location.field_path_elements:
                print('\t\tOn field: %s' % field_path_element.field_name)
    sys.exit(1)

Ruby

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

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

# Add budget.
return_budget = client.service.campaign_budget.mutate_campaign_budgets(
    customer_id, [operation])