Articles

 

Using SmartyStreets .NET SDK in C#

This article is a companion to the YouTube video produced by SmartyStreets on how to use the SmartyStreets .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.

SmartyStreets 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 SmartyStreets .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 SmartyStreets 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 SmartyStreets .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 SmartyStreets 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 SmartyStreets 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.

Helpful .NET SDK Links

This site uses cookies for analytics, personalized content, and ads. By continuing to browse this site, you agree to this use.
Consuming raw or undercooked cookie dough may increase your risk of foodborne illness.