This page has examples of how to use the Address Validation API client libraries to call the following services:
Install the client libraries
See Address Validation API client libraries for installation instructions.
Authentication
When you use client libraries, you use Application Default Credentials (ADC) to authenticate. For information about setting up ADC, see Provide credentials for Application Default Credentials. For information about using ADC with client libraries, see Authenticate using client libraries .
You can also use API keys to authenticate to the client libraries, for example:
using Google.Maps.AddressValidation.V1;
using Google.Api.Gax.Grpc;
using Grpc.Core;
...
// Create settings to pass the API key as a header in every request.
var apiHeader = CallSettings.FromHeader("X-Goog-Api-Key", "API_KEY");
var defaultSettings = AddressValidationSettings.GetDefault();
var settings = new AddressValidationSettings
{
ValidateAddressSettings = defaultSettings.ValidateAddressSettings.MergedWith(apiHeader),
ProvideValidationFeedbackSettings = defaultSettings.ProvideValidationFeedbackSettings.MergedWith(apiHeader)
};
// Create a client builder with the custom settings.
AddressValidationClientBuilder builder = new AddressValidationClientBuilder
{
Settings = settings,
// Use SslCredentials to create a secure channel for API key authentication.
ChannelCredentials = new SslCredentials()
};
AddressValidationClient client = await builder.BuildAsync();
When you use API keys in your applications, make sure that they are kept secure during both storage and transmission. Publicly exposing your API keys can lead to unexpected charges on your account.
The examples on this page use Application Default Credentials.
Examples
validateAddress
The following is an example of how to call
validateAddress
using the client library.
using Google.Maps.AddressValidation.V1;
using Google.Type;
...
private static async Task CallAddressValidation()
{
// Create the Address Validation Client
AddressValidationClient client = await AddressValidationClient.CreateAsync();
// Define the request with the address to be validated
var request = new ValidateAddressRequest
{
Address = new PostalAddress
{
RegionCode = "US",
LanguageCode = "en",
PostalCode = "94043",
AdministrativeArea = "CA",
Locality = "Mountain View",
AddressLines = { "1600 Amphitheatre Parkway" }
}
};
try
{
// Call the API asynchronously
ValidateAddressResponse response = await client.ValidateAddressAsync(request);
// Process the results
Console.WriteLine($"Validation Granularity: {response.Result.Verdict.ValidationGranularity}");
Console.WriteLine($"Formatted Address: {response.Result.Address.FormattedAddress}");
Console.WriteLine($"Response ID: {response.ResponseId}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
Define a PostalAddress
object with componentized address fields like
RegionCode
, Locality
, and AddressLines
. When building the example request,
this PostalAddress
is used to create a ValidateAddressRequest
. The
ValidateAddressAsync
method is then called to make the request, and details
from the response like the ValidationGranularity
and the FormattedAddress
are output.
provideValidationFeedback
The following is an example of how to call
provideValidationFeedback
using the client library.
using Google.Maps.AddressValidation.V1;
...
private static async Task ProvideValidationFeedback()
{
AddressValidationClient client = await AddressValidationClient.CreateAsync();
var feedbackRequest = new ProvideValidationFeedbackRequest
{
// Set the conclusion based on the user's choice. This exampels uses ValidatedVersionUsed
Conclusion = ProvideValidationFeedbackRequest.Types.ValidationConclusion.ValidatedVersionUsed,
// Provide the ID from the validation response.
ResponseId = "Response_ID"
};
try
{
Console.WriteLine("Sending feedback to the API");
// This call returns an empty response on success.
await client.ProvideValidationFeedbackAsync(feedbackRequest);
Console.WriteLine(" -> Feedback sent successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
Create a ProvideValidationFeedbackRequest
to send information about the final
outcome of an address validation sequence. The request requires a Conclusion
to specify whether the user's original address or Google's validated address was
used. Crucially, you must also provide the ResponseId
obtained from the
initial ValidateAddressResponse
to link the feedback to the correct
transaction.