Smarty

Using Smarty .NET SDK in C#

This article is a companion to the YouTube video produced by Smarty on how to use the Smarty .NET SDK in C#.

Its purpose it to show how to use the US Street API with multiple addresses using C#. Also see the Helpful Links section below for links to other APIs and examples.

Smarty provides a .NET SDK for use with all .NET compatible languages. This article specifically addresses using the SDK with C# in Visual Studio.

In this article, we'll discuss:

Register

Before you can use the Smarty .NET SDK, you will need to register for a free account. This will give you an auth-id and auth-token that you will use in the sample code below.

Once you register, login to your Smarty Account.
Then click API Keys at the top of the page to find your auth-id and auth-token. These will be used in the sample code later in the article.

Install the Smarty .NET SDK in Visual Studio

Installation of the SDK is simple.

  • Right-click your C# project in Visual Studio: Add -> Add NuGet Package
  • Search for smartystreets-dotnet-sdk
  • Click Add Package

US Street Multiple Addresses Sample Code

Note that this sample requires an auth-id and auth-token (see Register above).

namespace Examples {
	using System;
	using System.IO;
	using SmartyStreets;
	using SmartyStreets.USStreetApi;

	internal static class USStreetMultipleAddressesExample {
		public static void Main() {
			// You don't have to store your keys in environment variables, but we recommend it.
			var authId = "";
			var authToken = "";
			//var authId = Environment.GetEnvironmentVariable("smartystreets-auth-id");
			//var authToken = Environment.GetEnvironmentVariable("smartystreets-auth-token");
			var client = new ClientBuilder(authId, authToken).BuildUsStreetApiClient();
			var batch = new Batch();

			var address1 = new Lookup {
				Street = "1600 amphitheatre parkway",
				City = "Mountain view",
				State = "california"
			};

			var address2 = new Lookup("1 Rosedale, Baltimore, Maryland") {
				MaxCandidates = 10
			}; // Freeform addresses work too.

			var address3 = new Lookup("123 Bogus Street, Pretend Lake, Oklahoma");

			var address4 = new Lookup {
				Street = "1 Infinite Loop",
				ZipCode = "95014"
			};

			try {
				batch.Add(address1);
				batch.Add(address2);
				batch.Add(address3);
				batch.Add(address4);

				client.Send(batch);
			}
			catch (BatchFullException) {
				Console.WriteLine("Error. The batch is already full.");
			}
			catch (SmartyException ex) {
				Console.WriteLine(ex.Message);
				Console.WriteLine(ex.StackTrace);
			}
			catch (IOException ex) {
				Console.WriteLine(ex.StackTrace);
			}

			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 + " is valid. (There is at least one candidate)");

				foreach (var candidate in candidates) {
					var components = candidate.Components;
					var metadata = candidate.Metadata;

					Console.WriteLine("\nCandidate " + candidate.CandidateIndex + ":");
					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();
			}
		}
	}
}

Code Discussion

Specify your AuthID and AuthToken that you received when you registered for a free Smarty account. You can either hard-code the values, or create environment variables.

var authId = "";
var authToken = "";
//var authId = Environment.GetEnvironmentVariable("smartystreets-auth-id");
//var authToken = Environment.GetEnvironmentVariable("smartystreets-auth-token");

Next, you build the client object.
Also build a batch object to contain your requests.

var client = new ClientBuilder(authId, authToken).BuildUsStreetApiClient();
var batch = new Batch();

Create Lookup objects that will later be added to the batch request.
These lookup objects can vary depending on the information you have available. They can include address components as shown in address1. Or they can include freeform text such as in address2 and address3.

var address1 = new Lookup {
	Street = "1600 amphitheatre parkway",
	City = "Mountain view",
	State = "california"
};

var address2 = new Lookup("1 Rosedale, Baltimore, Maryland") {
	MaxCandidates = 10
}; // Freeform addresses work too.

var address3 = new Lookup("123 Bogus Street, Pretend Lake, Oklahoma");

var address4 = new Lookup {
	Street = "1 Infinite Loop",
	ZipCode = "95014"
};

Next, we add the Lookup objects to the batch, then send the batch to Smarty API servers.

batch.Add(address1);
batch.Add(address2);
batch.Add(address3);
batch.Add(address4);

client.Send(batch);

The remainder of the code is exception handling and iterating over the returned results. Side note, make sure you are also doing your own exception handling.

Ready to get started?