Skip to content

EasyPost Shipping API Client Library for .NET 3.5, 4.0 and 4.5

License

Notifications You must be signed in to change notification settings

roehnan/easypost-csharp

 
 

Repository files navigation

EasyPost .Net Client Library

EasyPost is a simple shipping API. You can sign up for an account at https://easypost.com

Documentation

Up-to-date documentation at: https://www.easypost.com/docs/api/csharp

Installation

The easiest way to add EasyPost to your project is with the NuGet package manager.

Install-Package EasyPost-Official

See NuGet docs for instructions on installing via the dialog or the console.

Usage

The EasyPost API consists of many object types. There are several attributes that are consistent across all objects:

  • id -- Guaranteed unique identifier of the object.
  • created_at/updated_at -- Timestamps of creation and last update time.

Configuration

If you are operating with a single EasyPost API key, during the initialization of your application add the following to configure EasyPost.

using EasyPost;

ClientManager.SetCurrent("ApiKey");

If you are operating with multiple EasyPost API keys, or wish to delegate the construction of the client requests, configure the ClientManager with a delegate at application initialization.

using EasyPost;

ClientManager.SetCurrent(() => new Client(new ClientConfiguration("yourApiKeyHere")));

An Address can be verified using one or many verifications methods. If Address is created without strict verifications the object will still be created, otherwise an HttpException will be raised.

using EasyPost;

Address address = new Address() {
    company = "Simpler Postage Inc",
    street1 = "164 Townsend Street",
    street2 = "Unit 1",
    city = "San Francisco",
    state = "CA",
    country = "US",
    zip = "94107",
    verify = new List<string>() { "delivery" }
};

address.Create();

if (address.verifications.delivery.success) {
    // successful verification
} else {
    // unsuccessful verification
}
using EasyPost;

Address address = new Address() {
    company = "Simpler Postage Inc",
    street1 = "164 Townsend Street",
    street2 = "Unit 1",
    city = "San Francisco",
    state = "CA",
    country = "US",
    zip = "94107",
    verify_strict = new List<string>() { "delivery" }
};

try {
    address.Create();
} except (HttpException) {
    // unsuccessful verification
}

// successful verification

Rating is available through the Shipment object. Since we do not charge for rating there are rate limits for this action if you do not eventually purchase the Shipment. Please contact us at [email protected] if you have any questions.

Address fromAddress = new Address() { zip = "14534" };
Address toAddress = new Address() { zip = "94107" };

Parcel parcel = new Parcel() {
    length = 8,
    width = 6,
    height = 5,
    weight = 10
};

Shipment shipment = new Shipment() {
    from_address = fromAddress,
    to_address = toAddress,
    parcel = parcel
};

shipment.Create();

foreach (Rate rate in shipment.rates) {
    // process rates
}

Postage Label Generation

Purchasing a shipment will generate a PostageLabel and any customs Forms that are needed for shipping.

Address fromAddress = new Address() { id = "adr_..." };
Address toAddress = new Address() {
    company = "EasyPost",
    street1 = "164 Townsend Street",
    street2 = "Unit 1",
    city = "San Francisco",
    state = "CA",
    country = "US",
    zip = "94107"
};

Parcel parcel = new Parcel() {
    length = 8,
    width = 6,
    height = 5,
    weight = 10
};

CustomsItem item = new CustomsItem() { description = "description" };
CustomsInfo info = new CustomsInfo() {
    customs_certify = "TRUE",
    eel_pfc = "NOEEI 30.37(a)",
    customs_items = new List<CustomsItem>() { item }
};

Options options = new Options() { label_format = "PDF" };

Shipment shipment = new Shipment() {
    from_address = fromAddress,
    to_address = toAddress,
    parcel = parcel,
    customs_info = info,
    options = options
};

shipment.Buy(shipment.LowestRate(
    includeServices: new List<string>() { "Priority" },
    excludeCarriers: new List<string>() { "USPS" }
));

shipment.postage_label.url; // https://easypost-files.s3-us-west-2.amazonaws.com/files/postage_label/20160826/8e77c397d47b4d088f1c684b7acd802a.png

foreach (Form form in shipment.forms) {
    // process forms
}

Asynchronous Batch Processing

The Batch object allows you to perform operations on multiple Shipments at once. This includes scheduling a Pickup, creating a ScanForm and consolidating labels. Operations performed on a Batch are asynchronous and take advantage of our webhoook infrastructure.

using EasyPost;

Shipment shipment = new Shipment() {
    from_address = fromAddress,
    to_address = toAddress,
    parcel = parcel,
    options = options
};

Batch batch = Batch.CreateAndBuy(new Dictionary<string, object>() {
    { "reference", "MyReference" },
    { "shipments", new List<Dictionary<string, object>>() { shipment } }
});

This will produce two webhooks. One batch.created and one batch.updated. Process each Batch state according to your business logic.

using EasyPost;

Batch batch = Batch.Retrieve("batch_...");
batch.GenerateLabel("zpl"); // populates batch.label_url asynchronously

Consume the subsequent batch.updated webhook to process further.

Releasing

  1. Update the CHANGELOG.
  2. Bump version in EasyPost.nuspec and VersionInfo.cs files.
  3. Rebuild the library to update dlls in lib directory.
  4. Create a git tag named the version number, e.g. 2.1.2.1, and push it.
  5. Publish new version on Nuget.

Note: Do not use RestSharp versions > 106.4.2. The RestRequest.RootElement functionality is broken in later versions and the client library depends on RootElement.

Reporting Issues

If you have an issue with the client feel free to open an issue on GitHub. If you have a general shipping question or a questions about EasyPost's service please contact [email protected] for additional assistance.

About

EasyPost Shipping API Client Library for .NET 3.5, 4.0 and 4.5

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%