Skip to content

EasyPost/easypost-csharp

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

- Fix typo on payment method charge endpoint
- Fix F# version hash issue
3c694e2

Git stats

Files

Permalink
Failed to load latest commit information.

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.Collections.Generic;
using System.Threading.Tasks;
using EasyPost;
using Newtonsoft.Json;

namespace example
{
    class exampleClass
    {
        static async Task Main()
        {
            Client client = new Client(Environment.GetEnvironmentVariable("EASYPOST_API_KEY"));

            Shipment shipment = await client.Shipment.Create(new Dictionary<string, object>()
            {
                {
                    "to_address", new Dictionary<string, object>()
                    {
                        { "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" }
                    }
                },
                {
                    "from_address", new Dictionary<string, object>()
                    {
                        { "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 Dictionary<string, object>()
                    {
                        { "length", 20.2 },
                        { "width", 10.9 },
                        { "height", 5 },
                        { "weight", 65.9 }
                    }
                }
            });

            await shipment.Buy(shipment.LowestRate());

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

Configuration

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

using EasyPost;

Client myClient = new Client("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 }
});

Functions involving a specific resource are then enacted on that resource. For example, to buy the shipment:

await myShipment.Buy(myShipment.LowestRate());

Any generated local resource will have stored internally the same client used to create or retrieve them. Any API call made against the resource will automatically use the same client. This will prevent potential issues of accidentally using the wrong API key when interacting with a resource in a multi-client environment.

Documentation

API Documentation can be found at: https://easypost.com/docs/api. Alternatively, you can view Fuget.

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

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

# Format project
make format

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

# Generate coverage reports
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_USER_PROD_API_KEY (eg: adding a credit card to a referral user)

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.

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:

About

EasyPost Shipping API Client Library for .NET and .NET Framework applications

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages