Smarty .NET SDK
A .NET client for Smarty’s address verification, geocoding, autocomplete, and data enrichment APIs.
The Smarty .NET SDK enables you to integrate address intelligence into your .NET applications with minimal boilerplate. Whether you’re validating millions of addresses, enriching them with precise geocodes, or powering an autocomplete form, the .NET SDK gives you a clear way to call every Smarty API.
Contents
- Installation
- Authentication
- Create an API client
- Create and send a lookup
- Examine results
- More examples
Installation
Install the SDK (usingNuGet (opens in new tab)):
dotnet add package smartystreets-dotnet-sdk
Import packages:
using SmartyStreets;
using SmartyStreets.USStreetApi;
Authentication
Smarty offers two kinds of credentials depending on where your code runs:
-
Server-side (backend, CLI, batch jobs)
Use Auth ID + Auth Token (a Secret Key pair). -
Client-side (browser, front-end, public UI)
Use Auth Web + Auth Referer (a Website Key pair restricted by domain).
You can find all keys in your Smarty account under:
Account → API Keys.
We recommend storing your credentials in environment variables for security:
export SMARTY_AUTH_ID="your-auth-id"
export SMARTY_AUTH_TOKEN="your-auth-token"
export SMARTY_AUTH_WEB="your-auth-web"
export SMARTY_AUTH_REFERER="your-auth-referer"
For details on authentication and embedded vs. secret keys, see our authentication documentation.
Create an API client
The SDK offers different client builders for each API.
Server-side client
Here’s an example for USStreetApi:
var authId = Environment.GetEnvironmentVariable("SMARTY_AUTH_ID");
var authToken = Environment.GetEnvironmentVariable("SMARTY_AUTH_TOKEN");
using var client = new ClientBuilder(authId, authToken)
.BuildUsStreetApiClient();
Browser/Frontend client
Here’s an example for InternationalAutocompleteApi:
var authWeb = Environment.GetEnvironmentVariable("SMARTY_AUTH_WEB");
var authReferer = Environment.GetEnvironmentVariable("SMARTY_AUTH_REFERER");
using var client = new ClientBuilder(authWeb,authReferer)
.BuildInternationalAutocompleteApiClient();
You can find a full list of API clients here (opens in new tab).
Create and send a lookup
Create lookups based on the addresses you want to validate. Here’s an example for
USStreetApi:
var lookup = new Lookup
{
Street = "1600 Amphitheatre Pkwy",
Secondary = "APT 2",
City = "Mountain View",
State = "CA",
ZipCode = "21229",
};
Use the client you created earlier to send the lookup:
try
{
client.Send(lookup);
}
catch (SmartyException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
return;
}
catch (IOException ex)
{
Console.WriteLine(ex.StackTrace);
return;
}
Examine results
var candidates = lookup.Result;
if (candidates.Count == 0)
{
Console.WriteLine("No candidates. This means the address is not valid.");
return;
}
var firstCandidate = candidates[0];
Console.WriteLine("Input ID: " + firstCandidate.InputId);
Console.WriteLine("ZIP Code: " + firstCandidate.Components.ZipCode);
Console.WriteLine("County: " + firstCandidate.Metadata.CountyName);
More examples
- US Street API (opens in new tab)
- US Autocomplete Pro API (opens in new tab)
- US Enrichment API (opens in new tab)
- US Extract API (opens in new tab)
- US Reverse Geocoding API (opens in new tab)
- US ZIP Code API (opens in new tab)
- International Autocomplete API (opens in new tab)
- International Street API (opens in new tab)
All .NET SDK examples can be found on our GitHub here (opens in new tab).