Google provides a .NET client library for interacting with the Ad Manager API.
We recommend using the client library with NuGet.

To get started, create a new project in the IDE of your choice or add the
dependency to an existing project. Google publishes client library artifacts to
NuGet repository as
[`Google.Ads.AdManager.V1`](https://www.nuget.org/packages/Google.Ads.AdManager.V1).

### Package Reference

```xml
<!-- MyProject.csproj -->
<PackageReference Include="Google.Ads.AdManager.V1" Version="1.0.0-beta14" />
```

### .NET CLI

```bash
dotnet add package Google.Ads.AdManager.V1 --version 1.0.0-beta14
```

## Configure credentials

The .NET client library uses OAuth2 and [Application Default Credentials](https://cloud.google.com/docs/authentication/application-default-credentials)
(ADC) to authenticate.

ADC searches for credentials in order in the following locations:

1. `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
2. User credentials set up through the Google Cloud CLI (gcloud CLI).
3. When running on Google Cloud, the service account attached to the Google Cloud resource.

For creating and configuring your ADC credentials, see
[Authentication](https://developers.google.com/ad-manager/api/beta/authentication.md.txt).

## Make your first request

Each service has a `ServiceClient` object with both synchronous and asynchronous
methods for each REST method. The following example reads a [`Network`](https://developers.google.com/ad-manager/api/beta/reference/rest/v1/networks.md.txt)
synchronously. All entities are identified by a
[resource name](https://developers.google.com/ad-manager/api/beta/client-libraries/dotnet.md.txt#construct_resource_names) consisting of the type and numeric
identifier of the entity.

```c#
    using Google.Ads.AdManager.V1;

    public sealed partial class GeneratedNetworkServiceClientSnippets
    {
        public void GetNetwork()
        {
            // Create client
            NetworkServiceClient networkServiceClient = NetworkServiceClient.Create();
            // Initialize request argument(s)
            string name = "networks/[NETWORK_CODE]";
            // Make the request
            Network response = networkServiceClient.GetNetwork(name);
        }
    }
```

For examples of other methods and resources, see the GitHub repository
[`googleapis/google-cloud-dotnet`](https://github.com/googleapis/google-cloud-dotnet/apis/Google.Ads.AdManager.V1/Google.Ads.AdManager.V1.GeneratedSnippets).

## Handle errors

All Ad Manager API errors are of the type
[Grpc.Core.RpcException](https://cloud.google.com/dotnet/docs/reference/Grpc.Core/latest/Grpc.Core.RpcException)
in the .NET client library.

Errors include a unique `request_id` you can
provide to [support](https://developers.google.com/ad-manager/api/beta/support) for assistance with
troubleshooting. The following example extracts the HTTP status, error message,
and `request_id`.

```c#
using Google.Ads.AdManager.V1;
using Google.Api.Gax.Grpc;
using Google.Rpc;
using Grpc.Core;

try
{
    NetworkServiceClient networkServiceClient = NetworkServiceClient.Create();
    Network network = networkServiceClient.GetNetwork("networks/[NETWORK_CODE]");
}
catch(RpcException e)
{
    // HTTP Status code
    StatusCode statusCode = e.Status.StatusCode;
    // Error message
    string errorMessage = e.Status.Detail;
    // Unique request identifier.
    RequestInfo requestInfo = e.GetStatusDetail<RequestInfo>();
    string requestId = requestInfo?.RequestId ?? "Unexpected null request identifier";
}

```

## Construct resource names

The client library provides helper classes for building resource names from
IDs. These can be passed directly to service methods.

```c#
//  Represents a resource name in the format:
//  "networks/{networkCode}/orders/{orderId}"
OrderName name = OrderName.FromNetworkCodeOrder("123", "456");
Order response = orderServiceClient.GetOrder(name);

```

## Configure proxy settings

The .NET client library respects the `HttpClient.DefaultProxy` property
including setting proxies from the environment variables `HTTP_PROXY` and
`HTTPS_PROXY`. For more details, see
[HttpClient.DefaultProxy Property](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.defaultproxy).