Sample code for 30+ languages & platforms
Dart

Shippo Create the Shipment, Get Rates, and Purchase Label

See more Shippo Examples

Demonstrates how retrieve rates and create labels for international shipments.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final http = CkHttp();

  // Implements the following CURL command:

  // curl https://api.goshippo.com/customs/declarations/ \
  //     -H "Authorization: ShippoToken <API_TOKEN>" \
  //     -H "Content-Type: application/json"  \
  //     -d '{          
  //           "contents_type": "MERCHANDISE",
  //           "non_delivery_option": "RETURN",
  //           "certify": true,
  //           "certify_signer": "Simon Kreuz",
  //           "incoterm": "DDU",
  //           "items": [{
  //                     "description": "T-shirt",
  //                     "quantity": 20,
  //                     "net_weight": "5",
  //                     "mass_unit": "lb",
  //                     "value_amount": "200",
  //                     "value_currency": "USD",
  //                     "tariff_number": "",
  //                     "origin_country": "US"
  //             }]
  //     }'

  // Use this online tool to generate code from sample JSON:
  // Generate Code to Create JSON

  // The following JSON is sent in the request body.

  // {
  //   "contents_type": "MERCHANDISE",
  //   "non_delivery_option": "RETURN",
  //   "certify": true,
  //   "certify_signer": "Simon Kreuz",
  //   "incoterm": "DDU",
  //   "items": [
  //     {
  //       "description": "T-shirt",
  //       "quantity": 20,
  //       "net_weight": "5",
  //       "mass_unit": "lb",
  //       "value_amount": "200",
  //       "value_currency": "USD",
  //       "tariff_number": "",
  //       "origin_country": "US"
  //     }
  //   ]
  // }

  final json = CkJsonObject();
  json.updateString('contents_type', 'MERCHANDISE');
  json.updateString('non_delivery_option', 'RETURN');
  json.updateBool('certify', true);
  json.updateString('certify_signer', 'Simon Kreuz');
  json.updateString('incoterm', 'DDU');
  json.updateString('items[0].description', 'T-shirt');
  json.updateInt('items[0].quantity', 20);
  json.updateString('items[0].net_weight', '5');
  json.updateString('items[0].mass_unit', 'lb');
  json.updateString('items[0].value_amount', '200');
  json.updateString('items[0].value_currency', 'USD');
  json.updateString('items[0].tariff_number', '');
  json.updateString('items[0].origin_country', 'US');

  http.setRequestHeader('Authorization', 'ShippoToken <API_TOKEN>');
  http.setRequestHeader('Content-Type', 'application/json');

  final resp = CkHttpResponse();
  try {
    http.httpJson('POST', 'https://api.goshippo.com/customs/declarations/', json, 'application/json', resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final sbResponseBody = CkStringBuilder();
  resp.getBodySb(sbResponseBody);
  final jResp = CkJsonObject();
  jResp.loadSb(sbResponseBody);
  jResp.emitCompact = false;

  print('Response Body:');
  print(jResp.emit());

  final respStatusCode = resp.statusCode;
  print('Response Status Code = $respStatusCode');
  if (respStatusCode >= 400) {
    print('Response Header:');
    print(resp.header);
    print('Failed.');
    return;
  }

  // Sample JSON response:
  // (Sample code for parsing the JSON response is shown below)

  // {
  //   "object_created": "2014-07-17T00:04:06.163Z",
  //   "object_updated": "2014-07-17T00:04:06.163Z",
  //   "object_id": "89436997a794439ab47999701e60392e",
  //   "object_owner": "shippotle@goshippo.com",
  //   "status": "SUCCESS",
  //   "address_from": {
  //     "object_id": "0943ae4e373e4120a99c337e496dcce8",
  //     "validation_results": {},
  //     "is_complete": true,
  //     "company": "",
  //     "street_no": "",
  //     "name": "Mr. Hippo",
  //     "street1": "215 Clayton St.",
  //     "street2": "",
  //     "city": "San Francisco",
  //     "state": "CA",
  //     "zip": "94117",
  //     "country": "US",
  //     "phone": "+15553419393",
  //     "email": "support@goshippo.com",
  //     "is_residential": null
  //   },
  //   "address_to": {
  //     "object_id": "4c7185d353764d0985a6a7825aed8ffb",
  //     "validation_results": {},
  //     "is_complete": true,
  //     "name": "Mrs. Hippo",
  //     "street1": "200 University Ave W",
  //     "city": "Waterloo",
  //     "state": "ON",
  //     "zip": "N2L 3G1",
  //     "country": "CA",
  //     "phone": "+1 555 341 9393",
  //     "email": "support@goshippo.com",
  //     "is_residential": false
  //   },
  //   "address_return": null,
  //   "parcels": [
  //     {
  //       "object_id": "ec952343dd4843c39b42aca620471fd5",
  //       "object_created": "2013-12-01T06:24:21.121Z",
  //       "object_updated": "2013-12-01T06:24:21.121Z",
  //       "object_owner": "shippotle@goshippo.com",
  //       "template": null,
  //       "length": "5",
  //       "width": "5",
  //       "height": "5",
  //       "distance_unit": "in",
  //       "weight": "2",
  //       "mass_unit": "lb",
  //       "value_amount": null,
  //       "value_currency": null,
  //       "metadata": "",
  //       "line_items": [
  //       ],
  //       "test": true
  //     }
  //   ],
  //   "shipment_date": "2013-12-03T12:00:00Z",
  //   "extra": {
  //     "insurance": {
  //       "currency": "",
  //       "amount": ""
  //     },
  //     "reference_1": "",
  //     "reference_2": ""
  //   },
  //   "customs_declaration": "b741b99f95e841639b54272834bc478c",
  //   "rates": [
  //     {
  //       "object_created": "2014-07-17T00:04:06.263Z",
  //       "object_id": "545ab0a1a6ea4c9f9adb2512a57d6d8b",
  //       "object_owner": "shippotle@goshippo.com",
  //       "shipment": "89436997a794439ab47999701e60392e",
  //       "attributes": [
  //       ],
  //       "amount": "5.50",
  //       "currency": "USD",
  //       "amount_local": "5.50",
  //       "currency_local": "USD",
  //       "provider": "USPS",
  //       "provider_image_75": "https://cdn2.goshippo.com/providers/75/USPS.png",
  //       "provider_image_200": "https://cdn2.goshippo.com/providers/200/USPS.png",
  //       "servicelevel": {
  //         "name": "Priority Mail",
  //         "token": "usps_priority",
  //         "terms": ""
  //       },
  //       "days": 2,
  //       "arrives_by": null,
  //       "duration_terms": "Delivery in 1 to 3 business days.",
  //       "messages": [
  //       ],
  //       "carrier_account": "078870331023437cb917f5187429b093",
  //       "test": false,
  //       "zone": 1
  //     },
  //     ...
  //   ],
  //   "carrier_accounts": [
  //   ],
  //   "messages": [
  //   ],
  //   "metadata": "Customer ID 123456"
  // }

  // Sample code for parsing the JSON response...
  // Use the following online tool to generate parsing code from sample JSON:
  // Generate Parsing Code from JSON

  var template = '';
  var length = '';
  var width = '';
  var height = '';
  var distanceUnit = '';
  var weight = '';
  var massUnit = '';
  var valueAmount = '';
  var valueCurrency = '';
  var j = 0;
  var countJ = 0;
  var intVal = 0;

  var objectCreated = jResp.stringOf('object_created');
  var objectUpdated = jResp.stringOf('object_updated');
  var objectId = jResp.stringOf('object_id');
  var objectOwner = jResp.stringOf('object_owner');
  final status = jResp.stringOf('status');
  final addressFromObjectId = jResp.stringOf('address_from.object_id');
  final addressFromCompany = jResp.stringOf('address_from.company');
  final addressFromStreetNo = jResp.stringOf('address_from.street_no');
  final addressFromName = jResp.stringOf('address_from.name');
  final addressFromStreet1 = jResp.stringOf('address_from.street1');
  final addressFromStreet2 = jResp.stringOf('address_from.street2');
  final addressFromCity = jResp.stringOf('address_from.city');
  final addressFromState = jResp.stringOf('address_from.state');
  final addressFromZip = jResp.stringOf('address_from.zip');
  final addressFromCountry = jResp.stringOf('address_from.country');
  final addressFromPhone = jResp.stringOf('address_from.phone');
  final addressFromEmail = jResp.stringOf('address_from.email');
  final addressFromIsResidential = jResp.stringOf('address_from.is_residential');
  final addressToObjectId = jResp.stringOf('address_to.object_id');
  final addressToName = jResp.stringOf('address_to.name');
  final addressToStreet1 = jResp.stringOf('address_to.street1');
  final addressToCity = jResp.stringOf('address_to.city');
  final addressToState = jResp.stringOf('address_to.state');
  final addressToZip = jResp.stringOf('address_to.zip');
  final addressToCountry = jResp.stringOf('address_to.country');
  final addressToPhone = jResp.stringOf('address_to.phone');
  final addressToEmail = jResp.stringOf('address_to.email');
  final addressReturn = jResp.stringOf('address_return');
  final shipmentDate = jResp.stringOf('shipment_date');
  final extraInsuranceCurrency = jResp.stringOf('extra.insurance.currency');
  final extraInsuranceAmount = jResp.stringOf('extra.insurance.amount');
  final extraReference_1 = jResp.stringOf('extra.reference_1');
  final extraReference_2 = jResp.stringOf('extra.reference_2');
  final customsDeclaration = jResp.stringOf('customs_declaration');
  var metadata = jResp.stringOf('metadata');
  var i = 0;
  var countI = jResp.sizeOfArray('parcels');
  while (i < countI) {
    jResp.i = i;
    objectId = jResp.stringOf('parcels[i].object_id');
    objectCreated = jResp.stringOf('parcels[i].object_created');
    objectUpdated = jResp.stringOf('parcels[i].object_updated');
    objectOwner = jResp.stringOf('parcels[i].object_owner');
    template = jResp.stringOf('parcels[i].template');
    length = jResp.stringOf('parcels[i].length');
    width = jResp.stringOf('parcels[i].width');
    height = jResp.stringOf('parcels[i].height');
    distanceUnit = jResp.stringOf('parcels[i].distance_unit');
    weight = jResp.stringOf('parcels[i].weight');
    massUnit = jResp.stringOf('parcels[i].mass_unit');
    valueAmount = jResp.stringOf('parcels[i].value_amount');
    valueCurrency = jResp.stringOf('parcels[i].value_currency');
    metadata = jResp.stringOf('parcels[i].metadata');
    jResp.boolOf('parcels[i].test');
    j = 0;
    countJ = jResp.sizeOfArray('parcels[i].line_items');
    while (j < countJ) {
      jResp.j = j;
      j++;
    }

    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('rates');
  while (i < countI) {
    jResp.i = i;
    intVal = jResp.intOf('rates[i]');
    j = 0;
    countJ = jResp.sizeOfArray('rates[i].attributes');
    while (j < countJ) {
      jResp.j = j;
      j++;
    }

    j = 0;
    countJ = jResp.sizeOfArray('rates[i].messages');
    while (j < countJ) {
      jResp.j = j;
      j++;
    }

    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('carrier_accounts');
  while (i < countI) {
    jResp.i = i;
    i++;
  }

  i = 0;
  countI = jResp.sizeOfArray('messages');
  while (i < countI) {
    jResp.i = i;
    i++;
  }
}