US Address Verification in Rust - Developer's quickstart guide
Welcome to the developer’s quickstart guide! This document will walk you through using the Rust 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 Rust SDK to your crate. This is an example cargo.toml file. Find the latest version at Smarty Rust SDK:
[package]name = "sdkdoc"version = "0.1.0"edition = "2021"[dependencies]smarty-rust-sdk = "1.2.0"tokio = { version = "1", features = ["full"] }serde_json = "1"Run cargo to add the SDKs and features to your crate.
cargo add smarty-rust-sdkcargo add tokio --features fullcargo add serde_jsonQuickstart
Import the Smarty SDK
Next, you’ll want to import the Smarty Rust SDK:
use smarty_rust_sdk::us_street_api::lookup::{Lookup, MatchStrategy};use smarty_rust_sdk::sdk::authentication::BasicAuthCredential;use smarty_rust_sdk::sdk::batch::Batch;use smarty_rust_sdk::sdk::options::OptionsBuilder;use smarty_rust_sdk::us_street_api::client::USStreetAddressClient;use std::error::Error;The us_street_api class provides the structures and endpoints to access Smarty’s US Address Verification and US Rooftop Geocoding products.
The snippets that follow use .await and the ? operator, so they belong inside an async main that returns a Result:
#[tokio::main]async fn main() -> Result<(), Box<dyn Error>> { // the code from the rest of this guide goes here Ok(())}Create an API client
Build your client object with your Smarty credentials (secret keys).
let authentication = BasicAuthCredential::new( std::env::var("SMARTY_AUTH_ID").expect("Missing SMARTY_AUTH_ID env variable"), std::env::var("SMARTY_AUTH_TOKEN").expect("Missing SMARTY_AUTH_TOKEN env variable"),);let options = OptionsBuilder::new(Some(authentication)) .with_logging() .build();let client = USStreetAddressClient::new(options)?;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 Rust 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.
let lookup = Lookup { street: "1600 Amphitheatre Pkwy".to_string(), last_line: "Mountain View, CA".to_string(), max_candidates: 10, match_strategy: MatchStrategy::Invalid, ..Default::default()};let lookup2 = Lookup { street: "1 Rosedale Street, Baltimore, MD".to_string(), max_candidates: 8, match_strategy: MatchStrategy::Invalid, ..Default::default()};let mut batch = Batch::default();batch.push(lookup)?;batch.push(lookup2)?;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
ORstreet and ZIP Code
ORstreet(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.
let lookup = Lookup{ street: "1600 Amphitheatre Parkway".to_string(), city: "Mountain View".to_string(), state: "CA".to_string(), zipcode: "94043".to_string(), ..Default::default()};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:
client.send(&mut batch).await?;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 record in batch.records() { println!("{}", serde_json::to_string_pretty(&record.results)?);}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?