EasyPost-Official 6.8.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package EasyPost-Official --version 6.8.1                
NuGet\Install-Package EasyPost-Official -Version 6.8.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EasyPost-Official" Version="6.8.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasyPost-Official --version 6.8.1                
#r "nuget: EasyPost-Official, 6.8.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EasyPost-Official as a Cake Addin
#addin nuget:?package=EasyPost-Official&version=6.8.1

// Install EasyPost-Official as a Cake Tool
#tool nuget:?package=EasyPost-Official&version=6.8.1                

EasyPost .Net Client Library

CI Coverage Status NuGet version

EasyPost, the simple shipping solution. You can sign up for an account at https://easypost.com.

Install

Install-Package EasyPost-Official

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

Usage

A simple create & buy shipment example:

using System;
using System.Threading.Tasks;
using EasyPost;
using Newtonsoft.Json;

namespace Example;

class ExampleClass
{
    static async Task Main()
    {
        var client = new Client(new ClientConfiguration(Environment.GetEnvironmentVariable("EASYPOST_API_KEY")));

        var createParameters = new EasyPost.Parameters.Shipment.Create()
        {
            ToAddress = new EasyPost.Parameters.Address.Create
            {
                Name = "Dr. Steve Brule",
                Street1 = "179 N Harbor Dr",
                City = "Redondo Beach",
                State = "CA",
                Zip = "90277",
                Country = "US",
                Phone = "8573875756",
                Email = "dr_steve_brule@gmail.com"
            },
            FromAddress = new EasyPost.Parameters.Address.Create
            {
                Name = "EasyPost",
                Street1 = "417 Montgomery Street",
                Street2 = "5th Floor",
                City = "San Francisco",
                State = "CA",
                Zip = "94104",
                Country = "US",
                Phone = "4153334445",
                Email = "support@easypost.com"
            },
            Parcel = new EasyPost.Parameters.Parcel.Create
            {
                Length = 20.2,
                Width = 10.9,
                Height = 5,
                Weight = 65.9
            }
        };

        var shipment = await client.Shipment.Create(createParameters);

        var rate = shipment.LowestRate();

        var buyParameters = new EasyPost.Parameters.Shipment.Buy(rate);

        var purchasedShipment = await client.Shipment.Buy(shipment.Id, buyParameters);

        Console.WriteLine(JsonConvert.SerializeObject(purchasedShipment, Formatting.Indented));
    }
}

Configuration

A Client object is the entry point into the EasyPost API. It is instantiated with a ClientConfiguration with your API key:

using EasyPost;

Client myClient = new Client(new ClientConfiguration("EASYPOST_API_KEY"));

An API key is required for all requests. You can find your API key in your EasyPost dashboard.

Once declared, a client's API key cannot be changed. If you are using multiple API keys, you can create multiple client objects.

Services

All general API services can be accessed through the Client object. For example, to access the Address service:

AddressService addressService = myClient.Address;

Beta services can be accessed via the myClient.Beta property.

ExampleService betaService = myClient.Beta.Example;

Resources

API objects cannot be created locally. All local objects are copies of server-side data, retrieved via an API call from a service.

For example, to create a new shipment, you must use the client's Shipment service:

Shipment myShipment = await myClient.Shipment.Create(new Dictionary<string, object>
{
    { "from_address", fromAddress },
    { "to_address", toAddress },
    { "parcel", parcel }
});

All API-calling functions are made from the appropriate service object (rather than against the resource object), by providing the ID of the related resource. For example, to buy a shipment:

Shipment myPurchasedShipment = await myClient.Shipment.Buy(myShipment.Id, myShipment.LowestRate());

Parameters

Most functions in this library accept a Dictionary<string, object> as their sole parameter, which is ultimately used as the body of the HTTP request against EasyPost's API. If you instead would like to use .NET objects to construct API call parameters, you can use the various Parameters classes.

For example, to create an address:

// Use an object constructor to create the address creation parameters
var addressCreateParameters = new EasyPost.Parameters.Address.Create {
    Name = "My Name",
    Street1 = "123 Main St",
    City = "San Francisco",
    State = "CA",
    Zip = "94105",
    Country = "US",
    Phone = "415-123-4567"
};

// You can add additional parameters as needed outside of the constructor
addressCreateParameters.Company = "My Company";

// Then convert the object to a dictionary
// This step will validate the data and throw an exception if there are any errors (i.e. missing required parameters)
var addressCreateDictionary = addressCreateParameters.ToDictionary();

// Pass the dictionary into the address creation method as normal
var address = await myClient.Address.Create(addressCreateDictionary);

Using the Parameters classes is not required, but they can help in a number of ways:

  • Naturally enforces parameter types (can't assign a string to an int parameter, for example)
  • Removes the need to remember parameter names (i.e. "name" vs "company")
  • Prevents typos in parameter names
  • Removes the need to know the exact JSON schema of the HTTP request body (parameters will be serialized into the proper schema behind-the-scenes)
  • Validates parameters (i.e. ensure required parameters are present)
  • Allows for IDE auto-completion
  • Allows for IDE parameter documentation
  • Provides a more natural way to construct parameters
  • Facilitates ASP.NET Core model binding (bind an HTML form to a Parameters instance)

HTTP Hooks

Users can audit the HTTP requests and responses being made by the library by setting the Hooks property of a ClientConfiguration with a set of event handlers. Available handlers include:

  • OnRequestExecuting - Called before an HTTP request is made. An OnRequestExecutingEventArgs object is passed to the handler, which contains details about the HttpRequestMessage that will be sent to the server.
    • The HttpRequestMessage at this point is configured with all expected data (headers, body, etc.). Modifying any data in the callback will NOT affect the actual request that is sent to the server.
  • OnRequestResponseReceived - Called after an HTTP request is made. An RequestResponseReceivedEventArgs object is passed to the handler, which contains details about the HttpResponseMessage that was received from the server.

Users can interact with these details in their callbacks as they see fit (e.g. logging).

void OnRequestExecutingHandler(object? sender, OnRequestExecutingEventArgs args) {
    // Interact with details about the HttpRequestMessage here via args
    System.Console.WriteLine($"Making HTTP call to {args.RequestUri}");
}

void OnRequestResponseReceivedHandler(object? sender, OnRequestResponseReceivedEventArgs args) {
    // Interact with details about the HttpResponseMessage here via args
    System.Console.WriteLine($"Received HTTP response with status code {args.ResponseStatusCode}");
}

Client client = new Client(new ClientConfiguration("EASYPOST_API_KEY")
{
    Hooks = new Hooks {
        OnRequestExecuting = OnRequestExecutingHandler,
        OnRequestResponseReceived = OnRequestResponseReceivedHandler,
    },
});

Users can subscribe to or unsubscribe from callbacks at any time via the Hooks property of a client.

// Add a new callback
client.Hooks.OnRequestExecuting += (sender, args) => { /* ... */ };

// Remove a callback
client.Hooks.OnRequestExecuting -= OnRequestExecutingHandler;

Proxies

If you need to use a proxy to make requests to the EasyPost API, you can define a custom HttpClientHandler on a custom HttpClient passed to the ClientConfiguration constructor.

// Define a custom HttpClientHandler with details about the proxy
HttpClientHandler handler = new()
{
    UseProxy = true,
    Proxy = new WebProxy($"http://localhost:8888"),
};

// Define a custom HttpClient with the custom handler
HttpClient httpClient = new(handler: handler);

// Pass the custom HttpClient to the ClientConfiguration constructor when creating a new EasyPost Client
Client client = new(new ClientConfiguration(FakeApikey)
{
    CustomHttpClient = httpClient,
});

Documentation

API documentation can be found at: https://docs.easypost.com.

Library documentation can be found on the web at: https://easypost.github.io/easypost-csharp or locally on the gh-pages branch.

Upgrading major versions of this project? Refer to the Upgrade Guide.

Support

New features and bug fixes are released on the latest major release of this library. If you are on an older major release of this library, we recommend upgrading to the most recent release to take advantage of new features, bug fixes, and security patches. Older versions of this library will continue to work and be available as long as the API version they are tied to remains active; however, they will not receive updates and are considered EOL.

For additional support, see our org-wide support policy.

Development

It is highly recommended to use a purpose-built IDE when working with this project such as Visual Studio. Most actions such as building, cleaning, and testing can be done via the GUI.

# Build project
make build

# Lint project
make lint
make lint-fix

# Run tests (recommended to instead run via an IDE like Visual Studio)
EASYPOST_TEST_API_KEY=123... EASYPOST_PROD_API_KEY=123... make test
EASYPOST_TEST_API_KEY=123... EASYPOST_PROD_API_KEY=123... make coverage

# Run security analysis
make scan
NuGet Dependencies

The NuGet package dependencies for this project are listed in the .csproj files. This project uses NuGet package locks to keep specific versions of dependencies. The lock files will be used during NuGet restore, if present.

If you need to update or alter NuGet dependencies, delete the package.lock.json files first. They will be regenerated during the next restore.

Testing

The test suite in this project was specifically built to produce consistent results on every run, regardless of when they run or who is running them. This project uses EasyVCR to record and replay HTTP requests and responses via "cassettes". When the suite is run, the HTTP requests and responses for each test function will be saved to a cassette if they do not exist already and replayed from this saved file if they do, which saves the need to make live API calls on every test run.

Sensitive Data: We've made every attempt to include scrubbers for sensitive data when recording cassettes so that PII or sensitive info does not persist in version control; however, please ensure when recording or re-recording cassettes that prior to committing your changes, no PII or sensitive information gets persisted by inspecting the cassette.

Making Changes: If you make an addition to this project, the request/response will get recorded automatically for you if UseVCR("testName"); is included on the test function. When making changes to this project, you'll need to re-record the associated cassette to force a new live API call for that test which will then record the request/response used on the next run.

Test Data: The test suite has been populated with various helpful fixtures that are available for use, each completely independent from a particular user with the exception of the USPS carrier account ID ( see Unit Test API Keys for more information) which has a fallback value of our internal testing user's ID. Some fixtures use hard-coded dates that may need to be incremented if cassettes get re-recorded (such as reports or pickups).

Unit Test API Keys

The following are required on every test run:

  • EASYPOST_TEST_API_KEY
  • EASYPOST_PROD_API_KEY

The following are required when you need to re-record cassettes for applicable tests (fallback values are used otherwise):

  • USPS_CARRIER_ACCOUNT_ID (eg: one-call buying a shipment for non-EasyPost employees)
  • REFERRAL_CUSTOMER_PROD_API_KEY (eg: adding a credit card to a referral customer)

Some tests may require a user with a particular set of enabled features such as a Partner user when creating referrals. We have attempted to call out these functions in their respective docstrings.

NOTE .NET Framework/.NET Standard unit tests cannot currently be run on Apple Silicon (M1, M2, etc.). Instead, run unit tests in one framework at a time with, e.g make unit-test fw=net8.0. Valid frameworks:

  • net462 (.NET Framework 4.6.2, the oldest non-EOL version of .NET Framework; will not run on Apple Silicon)
  • net5.0 (.NET 5.0)
  • net6.0 (.NET 6.0)
  • net7.0 (.NET 7.0)
  • net8.0 (.NET 8.0)
Test Coverage

Unit test coverage reports can be generated by running the generate_test_reports.sh Bash script from the root of this repository.

A report will be generated for each version of the library. Final reports will be stored in the coveragereport folder in the root of the repository following generation.

The script requires the following tools installed in your PATH:

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on EasyPost-Official:

Package Downloads
YeditepeSoft.Shipment

YeditepeSoft Shipment Library

EasyPost-Extensions

Package Description

Umbraco.Commerce.ShippingProviders.EasyPost

EasyPost shipping provider for Umbraco Commerce

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.9.0 1,486 11/22/2024
6.8.1 1,129 10/31/2024
6.8.0 554 10/22/2024
6.7.3 9,045 9/19/2024
6.7.2 5,122 8/16/2024
6.7.1 1,050 8/9/2024
6.7.0 4,385 7/24/2024
6.6.0 4,330 7/16/2024
6.5.2 6,216 6/12/2024
6.5.1 266 6/10/2024
6.5.0 1,124 6/5/2024
6.4.0 5,322 5/1/2024
6.3.1 6,877 4/12/2024
6.3.0 203 4/10/2024
6.2.1 2,821 3/18/2024
6.2.0 4,759 2/7/2024
6.1.0 5,602 1/8/2024
6.0.0 4,493 12/6/2023
5.8.0 4,878 10/11/2023
5.7.0 7,127 9/5/2023
5.6.0 672 8/31/2023
5.5.0 632 8/29/2023
5.4.1 2,898 8/17/2023
5.4.0 1,035 8/14/2023
5.3.0 1,150 7/28/2023
5.2.0 1,772 7/12/2023
5.1.0 9,387 6/6/2023
5.0.0 3,781 5/15/2023
4.6.2 2,907 5/15/2023
4.6.0 3,363 4/18/2023
4.5.0 2,009 3/22/2023
4.4.0 2,934 2/21/2023
4.3.0 1,951 1/18/2023
4.2.0 1,121 1/13/2023
4.1.0 4,788 12/7/2022
4.0.2 2,439 11/1/2022
4.0.1 5,836 10/24/2022
4.0.0 2,273 10/12/2022
3.6.1 3,996 9/22/2022
3.6.0 978 9/22/2022
3.5.0 3,038 8/25/2022
3.4.0 1,610 8/2/2022
3.3.0 3,001 7/18/2022
3.2.0 1,032 7/11/2022
3.1.0 11,971 5/19/2022
3.0.0 13,522 4/15/2022
2.8.3 10,351 7/1/2022
2.8.2 33,961 2/25/2022
2.8.1 4,574 2/17/2022
2.5.1.3 108,754 1/7/2020
2.5.1.2 21,335 9/29/2019
2.5.1.1 21,667 7/5/2019
2.5.1 27,603 10/9/2018
2.5.0.1 5,902 10/3/2018
2.5.0 5,717 9/28/2018
2.4.0 14,670 6/22/2018
2.3.1.4 18,485 1/9/2018
2.3.1.3 17,873 11/29/2017
2.3.1.2 29,786 5/16/2017
2.3.1.1 9,730 4/20/2017
2.3.1 6,710 3/28/2017
2.3.0 6,095 3/11/2017
2.2.7 6,135 3/7/2017
2.2.6 7,932 1/24/2017
2.2.5 6,224 1/24/2017
2.2.4 7,049 12/15/2016
2.2.3 6,918 12/13/2016
2.2.2 5,992 12/7/2016
2.2.1 11,771 8/26/2016
2.2.0 6,199 8/25/2016
2.1.2.1 6,388 8/19/2016
2.1.1 6,814 7/8/2016
2.1.0 7,839 5/9/2016
2.0.3.1 7,437 3/12/2016
2.0.3 6,176 3/3/2016
2.0.2.1 6,469 2/19/2016
2.0.2 6,605 2/10/2016
2.0.1.1 10,682 1/14/2016
2.0.1 7,192 1/14/2016
2.0.0 6,409 1/12/2016
1.2.2.2 7,696 12/18/2015
1.2.2.1 6,266 11/9/2015
1.2.2 6,063 11/4/2015
1.2.1 6,881 10/27/2015
1.2.0.1 6,124 10/6/2015
1.2.0 6,759 9/15/2015
1.1.7.2 5,977 9/10/2015
1.1.7.1 6,443 8/12/2015
1.1.7 7,539 6/26/2015
1.1.6 6,086 6/11/2015
1.1.5.2 6,062 6/5/2015
1.1.5.1 6,407 5/13/2015
1.1.4.5 6,228 3/20/2015
1.1.4.4 6,291 2/6/2015
1.1.4.3 6,041 2/6/2015
1.1.4.1 6,188 1/29/2015
1.1.4 6,198 1/29/2015
1.1.3.2 5,969 1/20/2015
1.1.3 6,457 12/17/2014
1.1.2.6 6,475 11/19/2014
1.1.2.5 6,280 11/17/2014
1.1.2.4 6,687 11/3/2014
1.1.2.3 6,418 11/3/2014
1.1.2.1 7,185 10/31/2014
1.1.2 8,946 10/31/2014
1.1.1.2 7,723 10/30/2014
1.1.0 8,595 10/7/2014
1.0.1.7 5,984 8/15/2014
1.0.1.6 5,983 7/11/2014
1.0.1.4 6,180 6/10/2014
1.0.1.3 5,947 6/6/2014