Skip to content

Latest commit

 

History

History
389 lines (279 loc) · 14.4 KB

File metadata and controls

389 lines (279 loc) · 14.4 KB

Shippo logo Shippo C# SDK

Shippo is a shipping API that connects you with multiple shipping carriers (such as USPS, UPS, DHL, Canada Post, Australia Post, and many others) through one interface.

You must register for a Shippo account to use our API. It's free to sign up. Only pay to print a live label, test labels are free.

To use the API, you must generate an API Token. In the following examples, replace <YOUR_API_KEY_HERE> with your own token.

For example.

apiKeyHeader:"shippo_test_595d9cb0c0e14497bf07e75ecfec6c6d"

Summary

Shippo external API.: Use this API to integrate with the Shippo service

Table of Contents

SDK Installation

NuGet

To add the NuGet package to a .NET project:

dotnet add package Shippo

Locally

To add a reference to a local instance of the SDK in a .NET project:

dotnet add reference Shippo/Shippo.csproj

SDK Example Usage

Example

using Shippo;
using Shippo.Models.Requests;
using Shippo.Models.Components;

var sdk = new ShippoSDK(
    apiKeyHeader: "<YOUR_API_KEY_HERE>",
    shippoApiVersion: "2018-02-08"
);

var res = await sdk.Addresses.ListAsync(
    page: 1,
    results: 5,
    shippoApiVersion: "2018-02-08"
);

// handle response

Custom HTTP Client

The C# SDK makes API calls using an ISpeakeasyHttpClient that wraps the native HttpClient. This client provides the ability to attach hooks around the request lifecycle that can be used to modify the request or handle errors and response.

The ISpeakeasyHttpClient interface allows you to either use the default SpeakeasyHttpClient that comes with the SDK, or provide your own custom implementation with customized configuration such as custom message handlers, timeouts, connection pooling, and other HTTP client settings.

The following example shows how to create a custom HTTP client with request modification and error handling:

using Shippo;
using Shippo.Utils;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

// Create a custom HTTP client
public class CustomHttpClient : ISpeakeasyHttpClient
{
    private readonly ISpeakeasyHttpClient _defaultClient;

    public CustomHttpClient()
    {
        _defaultClient = new SpeakeasyHttpClient();
    }

    public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken? cancellationToken = null)
    {
        // Add custom header and timeout
        request.Headers.Add("x-custom-header", "custom value");
        request.Headers.Add("x-request-timeout", "30");
        
        try
        {
            var response = await _defaultClient.SendAsync(request, cancellationToken);
            // Log successful response
            Console.WriteLine($"Request successful: {response.StatusCode}");
            return response;
        }
        catch (Exception error)
        {
            // Log error
            Console.WriteLine($"Request failed: {error.Message}");
            throw;
        }
    }

    public void Dispose()
    {
        _httpClient?.Dispose();
        _defaultClient?.Dispose();
    }
}

// Use the custom HTTP client with the SDK
var customHttpClient = new CustomHttpClient();
var sdk = new ShippoSDK(client: customHttpClient);
You can also provide a completely custom HTTP client with your own configuration:
using Shippo.Utils;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

// Custom HTTP client with custom configuration
public class AdvancedHttpClient : ISpeakeasyHttpClient
{
    private readonly HttpClient _httpClient;

    public AdvancedHttpClient()
    {
        var handler = new HttpClientHandler()
        {
            MaxConnectionsPerServer = 10,
            // ServerCertificateCustomValidationCallback = customCertValidation, // Custom SSL validation if needed
        };

        _httpClient = new HttpClient(handler)
        {
            Timeout = TimeSpan.FromSeconds(30)
        };
    }

    public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken? cancellationToken = null)
    {
        return await _httpClient.SendAsync(request, cancellationToken ?? CancellationToken.None);
    }

    public void Dispose()
    {
        _httpClient?.Dispose();
    }
}

var sdk = ShippoSDK.Builder()
    .WithClient(new AdvancedHttpClient())
    .Build();
For simple debugging, you can enable request/response logging by implementing a custom client:
public class LoggingHttpClient : ISpeakeasyHttpClient
{
    private readonly ISpeakeasyHttpClient _innerClient;

    public LoggingHttpClient(ISpeakeasyHttpClient innerClient = null)
    {
        _innerClient = innerClient ?? new SpeakeasyHttpClient();
    }

    public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken? cancellationToken = null)
    {
        // Log request
        Console.WriteLine($"Sending {request.Method} request to {request.RequestUri}");
        
        var response = await _innerClient.SendAsync(request, cancellationToken);
        
        // Log response
        Console.WriteLine($"Received {response.StatusCode} response");
        
        return response;
    }

    public void Dispose() => _innerClient?.Dispose();
}

var sdk = new ShippoSDK(client: new LoggingHttpClient());

The SDK also provides built-in hook support through the SDKConfiguration.Hooks system, which automatically handles BeforeRequestAsync, AfterSuccessAsync, and AfterErrorAsync hooks for advanced request lifecycle management.

Documentation

Review our full guides and references at https://docs.goshippo.com/.

Available Resources and Operations

Available methods
  • List - List all addresses
  • Create - Create a new address
  • Get - Retrieve an address
  • Validate - Validate an address
  • List - List all carrier parcel templates
  • Get - Retrieve a carrier parcel templates
  • List - List all customs declarations
  • Create - Create a new customs declaration
  • Get - Retrieve a customs declaration
  • List - List all customs items
  • Create - Create a new customs item
  • Get - Retrieve a customs item
  • List - List all manifests
  • Create - Create a new manifest
  • Get - Retrieve a manifest
  • List - List all orders
  • Create - Create a new order
  • Get - Retrieve an order
  • List - List all parcels
  • Create - Create a new parcel
  • Get - Retrieve an existing parcel
  • Create - Create a refund
  • List - List all refunds
  • Get - Retrieve a refund
  • List - List all service groups
  • Create - Create a new service group
  • Update - Update an existing service group
  • Delete - Delete a service group
  • List - List all shipments
  • Create - Create a new shipment
  • Get - Retrieve a shipment
  • List - List all Shippo Accounts
  • Create - Create a Shippo Account
  • Get - Retrieve a Shippo Account
  • Update - Update a Shippo Account
  • Create - Register a tracking webhook
  • Get - Get a tracking status
  • List - List all shipping labels
  • Create - Create a shipping label
  • Get - Retrieve a shipping label
  • List - List all user parcel templates
  • Create - Create a new user parcel template
  • Delete - Delete a user parcel template
  • Get - Retrieves a user parcel template
  • Update - Update an existing user parcel template

Development

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Feel free to open a PR or a Github issue as a proof of concept and we'll do our best to include it in a future release!

About Shippo

Connect with multiple different carriers, get discounted shipping labels, track parcels, and much more with just one integration. You can use your own carrier accounts or take advantage of our discounted rates with the Shippo carrier accounts. Using Shippo makes it easy to deal with multiple carrier integrations, rate shopping, tracking and other parts of the shipping workflow. We provide the API and web app for all your shipping needs.