The official AfterShip Tracking .net API library
$ dotnet add package AfterShipTrackingThis library allows you to quickly and easily use the AfterShip Tracking API via C#.
For updates to this library, see our GitHub release page.
If you need support using AfterShip products, please contact support@aftership.com.
Before you begin to integrate:
Using the [.NET Core command-line interface (CLI) tools]:
dotnet add package AfterShipTracking
Using the [NuGet Command Line Interface (CLI)]:
nuget install AfterShipTracking
From within Visual Studio:
For with Visual Studio Code:
Create AfterShip instance with options
| Name | Type | Required | Description |
|---|---|---|---|
| api_key | string | ✔ | Your AfterShip API key |
| auth_type | enum | Default value: AuthType.API_KEY AES authentication: AuthType.AES RSA authentication: AuthType.RSA | |
| api_secret | string | Required if the authentication type is AuthType.AES or AuthType.RSA | |
| domain | string | AfterShip API domain. Default value: https://api.aftership.com | |
| user_agent | string | User-defined user-agent string, please follow RFC9110 format standard. | |
| proxy | string | HTTP proxy URL to use for requests. Default value: null Example: http://192.168.0.100:8888 | |
| max_retry | number | Number of retries for each request. Default value: 2. Min is 0, Max is 10. | |
| timeout | number | Timeout for each request in milliseconds. |
using AfterShipTracking;
class Program
{
static void Main()
{
try
{
AfterShipClient client = new AfterShipClient(
apiKey: "YOUR_API_KEY",
apiSecret: "YOUR_AES_SECRET",
authenticationType: AfterShipConfiguration.AUTHENTICATION_TYPE_AES
);
GetTrackingByIdOptions options = new GetTrackingByIdOptions();
GetTrackingByIdResponse response = client.Tracking.GetTrackingById("valid_value",options);
if (response != null)
{
Console.WriteLine(response);
}
} catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
See the Rate Limit to understand the AfterShip rate limit policy.
The SDK will return an error object when there is any error during the request, with the following specification:
| Name | Type | Description |
|---|---|---|
| message | string | Detail message of the error |
| code | enum | Error code enum for API Error. |
| meta_code | number | API response meta code. |
| status_code | number | HTTP status code. |
| response_body | string | API response body. |
| response_header | object | API response header. |
| code | meta_code | status_code | message |
|---|---|---|---|
| INVALID_REQUEST | 400 | 400 | The request was invalid or cannot be otherwise served. |
| INVALID_JSON | 4001 | 400 | Invalid JSON data. |
| TRACKING_ALREADY_EXIST | 4003 | 400 | Tracking already exists. |
| TRACKING_DOES_NOT_EXIST | 4004 | 404 | Tracking does not exist. |
| TRACKING_NUMBER_INVALID | 4005 | 400 | The value of tracking_number is invalid. |
| TRACKING_REQUIRED | 4006 | 400 | tracking object is required. |
| TRACKING_NUMBER_REQUIRED | 4007 | 400 | tracking_number is required. |
| VALUE_INVALID | 4008 | 400 | The value of [field_name] is invalid. |
| VALUE_REQUIRED | 4009 | 400 | [field_name] is required. |
| SLUG_INVALID | 4010 | 400 | The value of slug is invalid. |
| MISSING_OR_INVALID_REQUIRED_FIELD | 4011 | 400 | Missing or invalid value of the required fields for this courier. Besides tracking_number, also required: [field_name] |
| BAD_COURIER | 4012 | 400 | The error message will be one of the following:1. Unable to import shipment as the carrier is not on your approved list for carrier auto-detection. Add the carrier here: https://admin.aftership.com/settings/couriers2. Unable to import shipment as we don't recognize the carrier from this tracking number.3. Unable to import shipment as the tracking number has an invalid format.4. Unable to import shipment as this carrier is no longer supported.5. Unable to import shipment as the tracking number does not belong to a carrier in that group. |
| INACTIVE_RETRACK_NOT_ALLOWED | 4013 | 400 | Retrack is not allowed. You can only retrack an inactive tracking. |
| NOTIFICATION_REQUIRED | 4014 | 400 | notification object is required. |
| ID_INVALID | 4015 | 400 | The value of id is invalid. |
| RETRACK_ONCE_ALLOWED | 4016 | 400 | Retrack is not allowed. You can only retrack each shipment once. |
| TRACKING_NUMBER_FORMAT_INVALID | 4017 | 400 | The format of tracking_number is invalid. |
| API_KEY_INVALID | 401 | 401 | The API Key is invalid. |
| REQUEST_NOT_ALLOWED | 403 | 403 | The request is understood, but it has been refused or access is not allowed. |
| NOT_FOUND | 404 | 404 | The URI requested is invalid or the resource requested does not exist. |
| TOO_MANY_REQUEST | 429 | 429 | You have exceeded the API call rate limit. The default limit is 10 requests per second. |
| INTERNAL_ERROR | 500 | 500 | Something went wrong on AfterShip's end. |
| INTERNAL_ERROR | 502 | 502 | Something went wrong on AfterShip's end. |
| INTERNAL_ERROR | 503 | 503 | Something went wrong on AfterShip's end. |
| INTERNAL_ERROR | 504 | 504 | Something went wrong on AfterShip's end. |
|
The AfterShip SDK has the following resource which are exactly the same as the API endpoints:
GET /couriers
GetCouriersOptions options = new GetCouriersOptions();
GetCouriersResponse response = client.Courier.GetCouriers(options);
if (response != null)
{
Console.WriteLine(response);
}
POST /couriers/detect
DetectCourierOptions options = new DetectCourierOptions();
DetectCourierRequest detectCourierRequest = new DetectCourierRequest();
detectCourierRequest.TrackingNumber = "valid_value";
options.DetectCourierRequest = detectCourierRequest;
DetectCourierResponse response = client.Courier.DetectCourier(options);
if (response != null)
{
Console.WriteLine(response);
}
GET /courier-connections
GetCourierConnectionsOptions options = new GetCourierConnectionsOptions();
GetCourierConnectionsResponse response = client.CourierConnection.GetCourierConnections(options);
if (response != null)
{
Console.WriteLine(response);
}
POST /courier-connections
PostCourierConnectionsOptions options = new PostCourierConnectionsOptions();
PostCourierConnectionsRequest postCourierConnectionsRequest = new PostCourierConnectionsRequest();
postCourierConnectionsRequest.CourierSlug = "valid_value";
postCourierConnectionsRequest.Credentials = new Dictionary<string, string>();
options.PostCourierConnectionsRequest = postCourierConnectionsRequest;
PostCourierConnectionsResponse response = client.CourierConnection.PostCourierConnections(options);
if (response != null)
{
Console.WriteLine(response);
}
GET /courier-connections/{id}
GetCourierConnectionsByIdOptions options = new GetCourierConnectionsByIdOptions();
GetCourierConnectionsByIdResponse response = client.CourierConnection.GetCourierConnectionsById("valid_value",options);
if (response != null)
{
Console.WriteLine(response);
}
PATCH /courier-connections/{id}
PutCourierConnectionsByIdOptions options = new PutCourierConnectionsByIdOptions();
PutCourierConnectionsByIdRequest putCourierConnectionsByIdRequest = new PutCourierConnectionsByIdRequest();
putCourierConnectionsByIdRequest.Credentials = new Dictionary<string, string>();
options.PutCourierConnectionsByIdRequest = putCourierConnectionsByIdRequest;
PutCourierConnectionsByIdResponse response = client.CourierConnection.PutCourierConnectionsById("valid_value",options);
if (response != null)
{
Console.WriteLine(response);
}
DELETE /courier-connections/{id}
DeleteCourierConnectionsByIdOptions options = new DeleteCourierConnectionsByIdOptions();
DeleteCourierConnectionsByIdResponse response = client.CourierConnection.DeleteCourierConnectionsById("valid_value",options);
if (response != null)
{
Console.WriteLine(response);
}
POST /estimated-delivery-date/predict
PredictOptions options = new PredictOptions();
EstimatedDeliveryDateRequest predictRequest = new EstimatedDeliveryDateRequest();
predictRequest.Slug = "valid_value";
EstimatedDeliveryDateRequestOriginAddress originAddress = new EstimatedDeliveryDateRequestOriginAddress();
predictRequest.OriginAddress = originAddress;
EstimatedDeliveryDateRequestDestinationAddress destinationAddress = new EstimatedDeliveryDateRequestDestinationAddress();
predictRequest.DestinationAddress = destinationAddress;
options.PredictRequest = predictRequest;
PredictResponse response = client.EstimatedDeliveryDate.Predict(options);
if (response != null)
{
Console.WriteLine(response);
}
POST /estimated-delivery-date/predict-batch
PredictBatchOptions options = new PredictBatchOptions();
PredictBatchRequest predictBatchRequest = new PredictBatchRequest();
options.PredictBatchRequest = predictBatchRequest;
PredictBatchResponse response = client.EstimatedDeliveryDate.PredictBatch(options);
if (response != null)
{
Console.WriteLine(response);
}
GET /trackings
GetTrackingsOptions options = new GetTrackingsOptions();
GetTrackingsResponse response = client.Tracking.GetTrackings(options);
if (response != null)
{
Console.WriteLine(response);
}
POST /trackings
CreateTrackingOptions options = new CreateTrackingOptions();
CreateTrackingRequest createTrackingRequest = new CreateTrackingRequest();
createTrackingRequest.TrackingNumber = "valid_value";
options.CreateTrackingRequest = createTrackingRequest;
CreateTrackingResponse response = client.Tracking.CreateTracking(options);
if (response != null)
{
Console.WriteLine(response);
}
GET /trackings/{id}
GetTrackingByIdOptions options = new GetTrackingByIdOptions();
GetTrackingByIdResponse response = client.Tracking.GetTrackingById("valid_value",options);
if (response != null)
{
Console.WriteLine(response);
}
PUT /trackings/{id}
UpdateTrackingByIdOptions options = new UpdateTrackingByIdOptions();
UpdateTrackingByIdRequest updateTrackingByIdRequest = new UpdateTrackingByIdRequest();
options.UpdateTrackingByIdRequest = updateTrackingByIdRequest;
UpdateTrackingByIdResponse response = client.Tracking.UpdateTrackingById("valid_value",options);
if (response != null)
{
Console.WriteLine(response);
}
DELETE /trackings/{id}
DeleteTrackingByIdOptions options = new DeleteTrackingByIdOptions();
DeleteTrackingByIdResponse response = client.Tracking.DeleteTrackingById("valid_value",options);
if (response != null)
{
Console.WriteLine(response);
}
POST /trackings/{id}/retrack
RetrackTrackingByIdOptions options = new RetrackTrackingByIdOptions();
RetrackTrackingByIdResponse response = client.Tracking.RetrackTrackingById("valid_value",options);
if (response != null)
{
Console.WriteLine(response);
}
POST /trackings/{id}/mark-as-completed
MarkTrackingCompletedByIdOptions options = new MarkTrackingCompletedByIdOptions();
MarkTrackingCompletedByIdRequest markTrackingCompletedByIdRequest = new MarkTrackingCompletedByIdRequest();
options.MarkTrackingCompletedByIdRequest = markTrackingCompletedByIdRequest;
MarkTrackingCompletedByIdResponse response = client.Tracking.MarkTrackingCompletedById("valid_value",options);
if (response != null)
{
Console.WriteLine(response);
}
If you get stuck, we're here to help:
Copyright (c) 2025 AfterShip
Licensed under the MIT license.