Skip to main content
Not sure which product you need?Let the wizard guide you
42 days freeStart trial
Smarty

US Address Verification in .NET - Developer's quickstart guide

Welcome to the developer’s quickstart guide! This document will walk you through using the .NET SDK to access our US Address Verification product, but the same pattern applies to other Smarty products.

Before getting started, it’s important to note that most SDKs are hosted in Smarty’s official GitHub organization. The JavaScript SDK, Rust SDK, and some examples for TypeScript, React, and Vue can be found here. Also, if you’re working with another language, we’ve got support for that, too. These repositories also have examples of calling other Smarty APIs.

In-depth docs of other Smarty APIs can be found here.

Ready, set, let’s go!

Install

To get started with verifying US addresses, add Smarty’s .NET SDK to your project. Find the latest version in the Smarty .NET SDK.

dotnet add package smartystreets-dotnet-sdk
Tell us what looks wrong and we'll take a look.

Quickstart

Import the Smarty SDK

Next, you’ll want to import the Smarty .NET SDK into a C# program.

using SmartyStreets;using SmartyStreets.USStreetApi;
Tell us what looks wrong and we'll take a look.

The us-street module provides the structures and endpoints to access Smarty’s US Address Verification and US Rooftop Geocoding products.

Create an API client

Build your client object with your Smarty credentials (secret keys).

var authId = Environment.GetEnvironmentVariable("SMARTY_AUTH_ID");var authToken = Environment.GetEnvironmentVariable("SMARTY_AUTH_TOKEN");using var client = new ClientBuilder(authId, authToken).BuildUsStreetApiClient();
Tell us what looks wrong and we'll take a look.

This client handles all HTTP complexities. The ClientBuilder provides a number of option methods, such as setting max retries, timeouts, base URL, etc. For authentication, we will use basic auth with the secret keys. We recommend storing your credentials in environment variables for security.

Create and batch lookups

The Smarty .NET SDK sends all address validation requests in batches for us-street-api and us-zipcode-api. To create a request, start by creating a batch of addresses to send for processing at Smarty. We've provided some sample addresses here, but feel free to load your own addresses however you'd like.

List<string> addresses = new List<string> { "1600 Pennsylvania Avenue Washington, DC", "1600 Amphitheatre Parkway Mountain View, CA 94043" };var batch = new Batch();foreach (string address in addresses){	var lookup = new Lookup(address);	batch.Add(lookup);}
Tell us what looks wrong and we'll take a look.

Here, we’ve created a batch by loading each address into a Lookup object. Next, we add those Lookups to a Batch. Typically, these addresses come from a file, database, etc., but we’ve left that part up to you, depending on your needs.

For each lookup, you can use a combination of at least:

  • street, city, and state
    OR
  • street and ZIP Code
    OR
  • street (full address in a single field)

As another example, Lookup could include street, city, state, and ZIP Code, even though breaking the address into these components is not needed to perform the lookup.

var lookup = new Lookup{    Street = "1600 Amphitheatre Parkway",    City = "Mountain View",    State = "CA",    ZipCode = "94043"};
Tell us what looks wrong and we'll take a look.

There are many other fields that allow you to customize your response, such as match type, candidates, inputID, etc. To explore all of these options, view the us-street-api documentation here.

Also note that each batch has a maximum size limit of 100; multiple concurrent batches can be sent.

Send batch

Once you have your Batch filled with Lookups, simply send the batch to get your validated results:

try{    client.Send(batch);}catch (SmartyException ex){    Console.WriteLine(ex.Message);    Console.WriteLine(ex.StackTrace);    return;}catch (IOException ex){    Console.WriteLine(ex.StackTrace);    return;}
Tell us what looks wrong and we'll take a look.

To keep your integration reliable, we recommend handling any errors returned by the API. You can find a list of response codes for the us-street-api here.

Examine results

We’ve included a sample showing how you might loop through all results and print them, but at this point, you’re free to process the output however you like!

Keep in mind that each Lookup could have multiple results.

for (var i = 0; i < batch.Count; i++){    var candidates = batch[i].Result;    if (candidates.Count == 0)    {        Console.WriteLine("Address " + i + " is invalid.\n");        continue;    }    Console.WriteLine("Address " + i + " has at least one candidate.\n If the match parameter is set to STRICT, the address is valid.\n Otherwise, check the Analysis output fields to see if the address is valid.");    foreach (var candidate in candidates)    {        var components = candidate.Components;        var metadata = candidate.Metadata;        Console.WriteLine("\nCandidate " + candidate.CandidateIndex + ":");        Console.WriteLine("Input ID: " + candidate.InputId);        Console.WriteLine("Delivery line 1: " + candidate.DeliveryLine1);        Console.WriteLine("Last line:       " + candidate.LastLine);        Console.WriteLine("ZIP Code:        " + components.ZipCode + "-" + components.Plus4Code);        Console.WriteLine("County:          " + metadata.CountyName);        Console.WriteLine("Latitude:        " + metadata.Latitude);        Console.WriteLine("Longitude:       " + metadata.Longitude);    }    Console.WriteLine();}
Tell us what looks wrong and we'll take a look.

Documentation

Most of the SDKs are available in Smarty’s official GitHub organization, including the JavaScript SDK, Rust SDK, and repositories for TypeScript, React, and Vue. These repositories also have examples of calling other Smarty APIs.

In-depth docs for other Smarty APIs can be found here.

Was this helpful?

Ready to get started?

This chat is powered by Help Scout. Chats are recorded. Privacy Policy