The .NET client library

  • The Google Ads API .NET client library is hosted on GitHub and distributed as a NuGet package, offering features to ease credential management and client creation.

  • The library officially supports .NET Framework 4.7.2+, .NET Standard 2.1, and .NET 8.0, with potential compatibility on other platforms that haven't been officially tested.

  • To get started with the API, follow the quick start guide to set up accounts and make your first call.

  • The library provides detailed guides on basic usage, authentication, configuration, logging, and utility classes for improved API usability.

  • For optimizing application performance, consult the performance tuning guide.

The .NET client library is hosted on GitHub and is distributed as a NuGet package. It offers several features to help you use the API, including easier management of credentials and creation of Google Ads API service clients.

The library supports the following .NET frameworks:

  • .NET Framework 4.7.2 and later (net472)
  • .NET Standard 2.1 (netstandard2.1)
  • .NET 8.0 (net8.0)

.NET Standard 2.1 is compatible with several other .NET versions and implementations, so the library might work fine on many other platforms. However, we haven't officially tested the compatibility and thus don't officially support these platforms. See the .NET Standard 2.1 documentation to learn more.

The Google Ads API .NET library uses Grpc.Net.Client as the default transport layer, with a fallback to Grpc.Core on unsupported platforms. While you can force the use of Grpc.Core by setting UseGrpcCore to true, note that Grpc.Core is deprecated and intended only for legacy maintenance. See the basic usage guide to learn more.

Installation

Install the Google.Ads.GoogleAds NuGet package in your project using the dotnet command-line tool:

dotnet add package Google.Ads.GoogleAds

To load configuration settings from appsettings.json, App.config, or environment variables, also install the Google.Ads.GoogleAds.Extensions package:

dotnet add package Google.Ads.GoogleAds.Extensions

Make your first API call

To use the Google Ads API, you need to perform several steps to get access to the API and configure your Google Ads accounts. Follow the quick start guide, which walks you through all the important steps including setting up your Google Ads accounts.

Basic usage

Initialize GoogleAdsConfig and GoogleAdsClient with your configuration settings, retrieve GoogleAdsServiceClient, and stream campaign rows with SearchStream:

using System;
using Google.Ads.Gax.Config;
using Google.Ads.GoogleAds.Config;
using Google.Ads.GoogleAds.Lib;
using Google.Ads.GoogleAds.V26.Errors;
using Google.Ads.GoogleAds.V26.Services;

GoogleAdsConfig config = new GoogleAdsConfig()
{
    OAuth2Mode = OAuth2Flow.APPLICATION,
    OAuth2ClientId = "INSERT_CLIENT_ID.apps.googleusercontent.com",
    OAuth2ClientSecret = "INSERT_CLIENT_SECRET",
    OAuth2RefreshToken = "INSERT_REFRESH_TOKEN",
    LoginCustomerId = "INSERT_LOGIN_CUSTOMER_ID_HERE"
};

GoogleAdsClient client = new GoogleAdsClient(config);
GoogleAdsServiceClient googleAdsService =
    client.GetService(Services.V26.GoogleAdsService);

string customerId = "INSERT_CUSTOMER_ID_HERE";
string query =
    "SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id";

try
{
    googleAdsService.SearchStream(
        customerId,
        query,
        delegate (SearchGoogleAdsStreamResponse response)
        {
            foreach (GoogleAdsRow row in response.Results)
            {
                Console.WriteLine(
                    $"Campaign ID {row.Campaign.Id}: {row.Campaign.Name}");
            }
        });
}
catch (GoogleAdsException ex)
{
    Console.WriteLine($"Request ID {ex.RequestId} failed: {ex.Message}");
}

Refer to the basic usage guide to learn more about how to use the .NET client library.

Authorization and authentication

The .NET client library supports multiple approaches to authentication and authorization, including OAuth 2.0 user credentials (for desktop and web applications), service accounts, Application Default Credentials (v24.1.0 and later), and manual credential injection (v27.0.0 and later). Refer to the authorization and authentication guide to learn more.

Configuration and logging

The .NET client library supports multiple configuration and logging options using runtime objects, appsettings.json or custom JSON files, environment variables, or App.config files. In case you need to modify the logging behavior of the client library, refer to the custom gRPC interceptor guide.

Utilities

The .NET client library includes utility classes to improve API usability, including media and video upload helpers (UploadVideo in v27.2.0 and later). Refer to the following guides to learn more:

Fine-tune your application performance

Refer to the performance tuning guide for recommendations on how to fine-tune your application's performance.