Sample code for 30+ languages & platforms
Java

Shippo Create Shipment Object

See more Shippo Examples

Create a Shipment object to retrieve rates. It represents a request to ship a given package from the sender to the recipient address.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

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

        Http http = new Http();

        // Implements the following CURL command:

        // curl https://api.goshippo.com/shipments/  \
        //     -H "Authorization: ShippoToken <API_TOKEN>" \
        //     -H "Content-Type: application/json"  \
        //     -d '{
        //        "address_from":{
        //           "name":"Mr. Hippo",
        //           "street1":"215 Clayton St.",
        //           "city":"San Francisco",
        //           "state":"CA",
        //           "zip":"94117",
        //           "country":"US"
        //        },
        //        "address_to":{
        //           "name":"Mrs. Hippo",
        //           "street1":"965 Mission St.",
        //           "city":"San Francisco",
        //           "state":"CA",
        //           "zip":"94105",
        //           "country":"US"
        //        },
        //        "parcels":[{
        //           "length":"5",
        //           "width":"5",
        //           "height":"5",
        //           "distance_unit":"in",
        //           "weight":"2",
        //           "mass_unit":"lb"
        //        }],
        //        "async": false
        //     }'

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

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

        // {
        //   "address_from": {
        //     "name": "Mr. Hippo",
        //     "street1": "215 Clayton St.",
        //     "city": "San Francisco",
        //     "state": "CA",
        //     "zip": "94117",
        //     "country": "US"
        //   },
        //   "address_to": {
        //     "name": "Mrs. Hippo",
        //     "street1": "965 Mission St.",
        //     "city": "San Francisco",
        //     "state": "CA",
        //     "zip": "94105",
        //     "country": "US"
        //   },
        //   "parcels": [
        //     {
        //       "length": "5",
        //       "width": "5",
        //       "height": "5",
        //       "distance_unit": "in",
        //       "weight": "2",
        //       "mass_unit": "lb"
        //     }
        //   ],
        //   "async": false
        // }

        JsonObject json = new JsonObject();
        json.updateString("address_from.name", "Mr. Hippo");
        json.updateString("address_from.street1", "215 Clayton St.");
        json.updateString("address_from.city", "San Francisco");
        json.updateString("address_from.state", "CA");
        json.updateString("address_from.zip", "94117");
        json.updateString("address_from.country", "US");
        json.updateString("address_to.name", "Mrs. Hippo");
        json.updateString("address_to.street1", "965 Mission St.");
        json.updateString("address_to.city", "San Francisco");
        json.updateString("address_to.state", "CA");
        json.updateString("address_to.zip", "94105");
        json.updateString("address_to.country", "US");
        json.updateString("parcels[0].length", "5");
        json.updateString("parcels[0].width", "5");
        json.updateString("parcels[0].height", "5");
        json.updateString("parcels[0].distance_unit", "in");
        json.updateString("parcels[0].weight", "2");
        json.updateString("parcels[0].mass_unit", "lb");
        json.updateBool("async", false);

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

        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", "https://api.goshippo.com/shipments/", json, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        StringBuilder sbResponseBody = new StringBuilder();
        resp.getBodySb(sbResponseBody);
        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);
        jResp.setEmitCompact(false);

        System.out.println("Response Body:");
        System.out.println(jResp.emit());

        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.println("Failed.");
            return;
        }

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

        // {
        //   "carrier_accounts": [
        //   ],
        //   "object_created": "2019-06-28T17:45:20.441Z",
        //   "object_updated": "2019-06-28T17:45:20.511Z",
        //   "object_id": "17b01795a4884d8a9002ccc17c9d09d1",
        //   "object_owner": "admin@chilkatsoft.com",
        //   "status": "SUCCESS",
        //   "address_from": {
        //     "object_id": "427489906b604f498f7bd2429ab7d9a1",
        //     "is_complete": true,
        //     "name": "Mr. Hippo",
        //     "company": "",
        //     "street_no": "",
        //     "street1": "215 Clayton St.",
        //     "validation_results": {},
        //     "street2": "",
        //     "street3": "",
        //     "city": "San Francisco",
        //     "state": "CA",
        //     "zip": "94117",
        //     "country": "US",
        //     "phone": "",
        //     "email": "",
        //     "is_residential": null,
        //     "test": true
        //   },
        //   "address_to": {
        //     "object_id": "3d7166269a2844b7842ef9ace942ec7c",
        //     "is_complete": true,
        //     "name": "Mrs. Hippo",
        //     "company": "",
        //     "street_no": "",
        //     "street1": "965 Mission St.",
        //     "validation_results": {},
        //     "street2": "",
        //     "street3": "",
        //     "city": "San Francisco",
        //     "state": "CA",
        //     "zip": "94105",
        //     "country": "US",
        //     "phone": "",
        //     "email": "",
        //     "is_residential": null,
        //     "test": true
        //   },
        //   "parcels": [
        //     {
        //       "object_state": "VALID",
        //       "object_created": "2019-06-28T17:45:20.411Z",
        //       "object_updated": "2019-06-28T17:45:20.455Z",
        //       "object_id": "5af766ff15684a4186b0e3c833348fac",
        //       "object_owner": "admin@chilkatsoft.com",
        //       "template": null,
        //       "extra": {},
        //       "length": "5.0000",
        //       "width": "5.0000",
        //       "height": "5.0000",
        //       "distance_unit": "in",
        //       "weight": "2.0000",
        //       "mass_unit": "lb",
        //       "value_amount": null,
        //       "value_currency": null,
        //       "metadata": "",
        //       "line_items": [
        //       ],
        //       "test": true
        //     }
        //   ],
        //   "shipment_date": "2019-06-28T17:45:20.511Z",
        //   "address_return": {
        //     "object_id": "427489906b604f498f7bd2429ab7d9a1",
        //     "is_complete": true,
        //     "name": "Mr. Hippo",
        //     "company": "",
        //     "street_no": "",
        //     "street1": "215 Clayton St.",
        //     "validation_results": {},
        //     "street2": "",
        //     "street3": "",
        //     "city": "San Francisco",
        //     "state": "CA",
        //     "zip": "94117",
        //     "country": "US",
        //     "phone": "",
        //     "email": "",
        //     "is_residential": null,
        //     "test": true
        //   },
        //   "alternate_address_to": null,
        //   "customs_declaration": null,
        //   "extra": {},
        //   "rates": [
        //     {
        //       "object_created": "2019-06-28T17:45:20.789Z",
        //       "object_id": "dd7556c284e8444294d2ab7099e662d2",
        //       "object_owner": "admin@chilkatsoft.com",
        //       "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
        //       "attributes": [
        //         "FASTEST"
        //       ],
        //       "amount": "22.78",
        //       "currency": "USD",
        //       "amount_local": "22.78",
        //       "currency_local": "USD",
        //       "provider": "USPS",
        //       "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
        //       "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
        //       "servicelevel": {
        //         "name": "Priority Mail Express",
        //         "token": "usps_priority_express",
        //         "terms": ""
        //       },
        //       "estimated_days": 2,
        //       "arrives_by": null,
        //       "duration_terms": "Overnight delivery to most U.S. locations.",
        //       "messages": [
        //       ],
        //       "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
        //       "test": true,
        //       "zone": "1"
        //     },
        //     {
        //       "object_created": "2019-06-28T17:45:20.786Z",
        //       "object_id": "ec11cc297e2f4583986097f6d409e5c6",
        //       "object_owner": "admin@chilkatsoft.com",
        //       "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
        //       "attributes": [
        //         "BESTVALUE",
        //         "CHEAPEST",
        //         "FASTEST"
        //       ],
        //       "amount": "6.95",
        //       "currency": "USD",
        //       "amount_local": "6.95",
        //       "currency_local": "USD",
        //       "provider": "USPS",
        //       "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
        //       "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
        //       "servicelevel": {
        //         "name": "Priority Mail",
        //         "token": "usps_priority",
        //         "terms": ""
        //       },
        //       "estimated_days": 2,
        //       "arrives_by": null,
        //       "duration_terms": "Delivery within 1, 2, or 3 days based on where your package started and where it’s being sent.",
        //       "messages": [
        //       ],
        //       "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
        //       "test": true,
        //       "zone": "1"
        //     },
        //     {
        //       "object_created": "2019-06-28T17:45:20.785Z",
        //       "object_id": "d256586f539f4c93a2f02cc11f4942ac",
        //       "object_owner": "admin@chilkatsoft.com",
        //       "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
        //       "attributes": [
        //       ],
        //       "amount": "7.32",
        //       "currency": "USD",
        //       "amount_local": "7.32",
        //       "currency_local": "USD",
        //       "provider": "USPS",
        //       "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
        //       "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
        //       "servicelevel": {
        //         "name": "Parcel Select",
        //         "token": "usps_parcel_select",
        //         "terms": ""
        //       },
        //       "estimated_days": 7,
        //       "arrives_by": null,
        //       "duration_terms": "Delivery in 2 to 8 days.",
        //       "messages": [
        //       ],
        //       "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
        //       "test": true,
        //       "zone": "1"
        //     }
        //   ],
        //   "messages": [
        //     {
        //       "source": "DHLExpress",
        //       "code": "",
        //       "text": "Shippo's DHL Express master account doesn't support shipments to inside of the US"
        //     }
        //   ],
        //   "metadata": "",
        //   "test": true,
        //   "order": null
        // }

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

        String object_state = "";
        String template = "";
        String length = "";
        String width = "";
        String height = "";
        String distance_unit = "";
        String weight = "";
        String mass_unit = "";
        String value_amount = "";
        String value_currency = "";
        int j = 0;
        int count_j = 0;
        String shipment = "";
        String amount = "";
        String currency = "";
        String amount_local = "";
        String currency_local = "";
        String provider = "";
        String provider_image_75 = "";
        String provider_image_200 = "";
        String servicelevelName = "";
        String servicelevelToken = "";
        String servicelevelTerms = "";
        int estimated_days = 0;
        String arrives_by = "";
        String duration_terms = "";
        String carrier_account = "";
        String zone = "";
        String strVal = "";
        String source = "";
        String code = "";
        String text = "";

        String object_created = jResp.stringOf("object_created");
        String object_updated = jResp.stringOf("object_updated");
        String object_id = jResp.stringOf("object_id");
        String object_owner = jResp.stringOf("object_owner");
        String status = jResp.stringOf("status");
        String address_fromObject_id = jResp.stringOf("address_from.object_id");
        boolean address_fromIs_complete = jResp.boolOf("address_from.is_complete");
        String address_fromName = jResp.stringOf("address_from.name");
        String address_fromCompany = jResp.stringOf("address_from.company");
        String address_fromStreet_no = jResp.stringOf("address_from.street_no");
        String address_fromStreet1 = jResp.stringOf("address_from.street1");
        String address_fromStreet2 = jResp.stringOf("address_from.street2");
        String address_fromStreet3 = jResp.stringOf("address_from.street3");
        String address_fromCity = jResp.stringOf("address_from.city");
        String address_fromState = jResp.stringOf("address_from.state");
        String address_fromZip = jResp.stringOf("address_from.zip");
        String address_fromCountry = jResp.stringOf("address_from.country");
        String address_fromPhone = jResp.stringOf("address_from.phone");
        String address_fromEmail = jResp.stringOf("address_from.email");
        String address_fromIs_residential = jResp.stringOf("address_from.is_residential");
        boolean address_fromTest = jResp.boolOf("address_from.test");
        String address_toObject_id = jResp.stringOf("address_to.object_id");
        boolean address_toIs_complete = jResp.boolOf("address_to.is_complete");
        String address_toName = jResp.stringOf("address_to.name");
        String address_toCompany = jResp.stringOf("address_to.company");
        String address_toStreet_no = jResp.stringOf("address_to.street_no");
        String address_toStreet1 = jResp.stringOf("address_to.street1");
        String address_toStreet2 = jResp.stringOf("address_to.street2");
        String address_toStreet3 = jResp.stringOf("address_to.street3");
        String address_toCity = jResp.stringOf("address_to.city");
        String address_toState = jResp.stringOf("address_to.state");
        String address_toZip = jResp.stringOf("address_to.zip");
        String address_toCountry = jResp.stringOf("address_to.country");
        String address_toPhone = jResp.stringOf("address_to.phone");
        String address_toEmail = jResp.stringOf("address_to.email");
        String address_toIs_residential = jResp.stringOf("address_to.is_residential");
        boolean address_toTest = jResp.boolOf("address_to.test");
        String shipment_date = jResp.stringOf("shipment_date");
        String address_returnObject_id = jResp.stringOf("address_return.object_id");
        boolean address_returnIs_complete = jResp.boolOf("address_return.is_complete");
        String address_returnName = jResp.stringOf("address_return.name");
        String address_returnCompany = jResp.stringOf("address_return.company");
        String address_returnStreet_no = jResp.stringOf("address_return.street_no");
        String address_returnStreet1 = jResp.stringOf("address_return.street1");
        String address_returnStreet2 = jResp.stringOf("address_return.street2");
        String address_returnStreet3 = jResp.stringOf("address_return.street3");
        String address_returnCity = jResp.stringOf("address_return.city");
        String address_returnState = jResp.stringOf("address_return.state");
        String address_returnZip = jResp.stringOf("address_return.zip");
        String address_returnCountry = jResp.stringOf("address_return.country");
        String address_returnPhone = jResp.stringOf("address_return.phone");
        String address_returnEmail = jResp.stringOf("address_return.email");
        String address_returnIs_residential = jResp.stringOf("address_return.is_residential");
        boolean address_returnTest = jResp.boolOf("address_return.test");
        String alternate_address_to = jResp.stringOf("alternate_address_to");
        String customs_declaration = jResp.stringOf("customs_declaration");
        String metadata = jResp.stringOf("metadata");
        boolean test = jResp.boolOf("test");
        String order = jResp.stringOf("order");
        int i = 0;
        int count_i = jResp.sizeOfArray("carrier_accounts");
        while (i < count_i) {
            jResp.setI(i);
            i = i + 1;
        }

        i = 0;
        count_i = jResp.sizeOfArray("parcels");
        while (i < count_i) {
            jResp.setI(i);
            object_state = jResp.stringOf("parcels[i].object_state");
            object_created = jResp.stringOf("parcels[i].object_created");
            object_updated = jResp.stringOf("parcels[i].object_updated");
            object_id = jResp.stringOf("parcels[i].object_id");
            object_owner = 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");
            distance_unit = jResp.stringOf("parcels[i].distance_unit");
            weight = jResp.stringOf("parcels[i].weight");
            mass_unit = jResp.stringOf("parcels[i].mass_unit");
            value_amount = jResp.stringOf("parcels[i].value_amount");
            value_currency = jResp.stringOf("parcels[i].value_currency");
            metadata = jResp.stringOf("parcels[i].metadata");
            test = jResp.boolOf("parcels[i].test");
            j = 0;
            count_j = jResp.sizeOfArray("parcels[i].line_items");
            while (j < count_j) {
                jResp.setJ(j);
                j = j + 1;
            }

            i = i + 1;
        }

        i = 0;
        count_i = jResp.sizeOfArray("rates");
        while (i < count_i) {
            jResp.setI(i);
            object_created = jResp.stringOf("rates[i].object_created");
            object_id = jResp.stringOf("rates[i].object_id");
            object_owner = jResp.stringOf("rates[i].object_owner");
            shipment = jResp.stringOf("rates[i].shipment");
            amount = jResp.stringOf("rates[i].amount");
            currency = jResp.stringOf("rates[i].currency");
            amount_local = jResp.stringOf("rates[i].amount_local");
            currency_local = jResp.stringOf("rates[i].currency_local");
            provider = jResp.stringOf("rates[i].provider");
            provider_image_75 = jResp.stringOf("rates[i].provider_image_75");
            provider_image_200 = jResp.stringOf("rates[i].provider_image_200");
            servicelevelName = jResp.stringOf("rates[i].servicelevel.name");
            servicelevelToken = jResp.stringOf("rates[i].servicelevel.token");
            servicelevelTerms = jResp.stringOf("rates[i].servicelevel.terms");
            estimated_days = jResp.intOf("rates[i].estimated_days");
            arrives_by = jResp.stringOf("rates[i].arrives_by");
            duration_terms = jResp.stringOf("rates[i].duration_terms");
            carrier_account = jResp.stringOf("rates[i].carrier_account");
            test = jResp.boolOf("rates[i].test");
            zone = jResp.stringOf("rates[i].zone");
            j = 0;
            count_j = jResp.sizeOfArray("rates[i].attributes");
            while (j < count_j) {
                jResp.setJ(j);
                strVal = jResp.stringOf("rates[i].attributes[j]");
                j = j + 1;
            }

            j = 0;
            count_j = jResp.sizeOfArray("rates[i].messages");
            while (j < count_j) {
                jResp.setJ(j);
                j = j + 1;
            }

            i = i + 1;
        }

        i = 0;
        count_i = jResp.sizeOfArray("messages");
        while (i < count_i) {
            jResp.setI(i);
            source = jResp.stringOf("messages[i].source");
            code = jResp.stringOf("messages[i].code");
            text = jResp.stringOf("messages[i].text");
            i = i + 1;
        }
    }
}