Sample code for 30+ languages & platforms
Java

Shippo Create a Batch

See more Shippo Examples

Demonstrates how to create and purchase up to 10,000 shipments in a single API request.

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();

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

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

        // {
        //   "default_carrier_account": "078870331023437cb917f5187429b093",
        //   "default_servicelevel_token": "usps_priority",
        //   "label_filetype": "PDF_4x6",
        //   "metadata": "BATCH #170",
        //   "batch_shipments": [
        //     {
        //       "shipment": {
        //         "address_from": {
        //           "name": "Mr Hippo",
        //           "street1": "965 Mission St",
        //           "street2": "Ste 201",
        //           "city": "San Francisco",
        //           "state": "CA",
        //           "zip": "94103",
        //           "country": "US",
        //           "phone": "4151234567",
        //           "email": "mrhippo@goshippo.com"
        //         },
        //         "address_to": {
        //           "name": "Mrs Hippo",
        //           "company": "",
        //           "street1": "Broadway 1",
        //           "street2": "",
        //           "city": "New York",
        //           "state": "NY",
        //           "zip": "10007",
        //           "country": "US",
        //           "phone": "4151234567",
        //           "email": "mrshippo@goshippo.com"
        //         },
        //         "parcels": [
        //           {
        //             "length": "5",
        //             "width": "5",
        //             "height": "5",
        //             "distance_unit": "in",
        //             "weight": "2",
        //             "mass_unit": "oz"
        //           }
        //         ]
        //       }
        //     },
        //     {
        //       "shipment": {
        //         "address_from": {
        //           "name": "Mr Hippo",
        //           "street1": "1092 Indian Summer Ct",
        //           "city": "San Jose",
        //           "state": "CA",
        //           "zip": "95122",
        //           "country": "US",
        //           "phone": "4151234567",
        //           "email": "mrhippo@goshippo.com"
        //         },
        //         "address_to": {
        //           "name": "Mrs Hippo",
        //           "company": "",
        //           "street1": "Broadway 1",
        //           "street2": "",
        //           "city": "New York",
        //           "state": "NY",
        //           "zip": "10007",
        //           "country": "US",
        //           "phone": "4151234567",
        //           "email": "mrshippo@goshippo.com"
        //         },
        //         "parcels": [
        //           {
        //             "length": "5",
        //             "width": "5",
        //             "height": "5",
        //             "distance_unit": "in",
        //             "weight": "20",
        //             "mass_unit": "lb"
        //           }
        //         ]
        //       },
        //       "carrier_account": "a4391cd4ab974f478f55dc08b5c8e3b3",
        //       "servicelevel_token": "fedex_2_day"
        //     }
        //   ]
        // }

        JsonObject json = new JsonObject();
        json.updateString("default_carrier_account", "078870331023437cb917f5187429b093");
        json.updateString("default_servicelevel_token", "usps_priority");
        json.updateString("label_filetype", "PDF_4x6");
        json.updateString("metadata", "BATCH #170");
        json.updateString("batch_shipments[0].shipment.address_from.name", "Mr Hippo");
        json.updateString("batch_shipments[0].shipment.address_from.street1", "965 Mission St");
        json.updateString("batch_shipments[0].shipment.address_from.street2", "Ste 201");
        json.updateString("batch_shipments[0].shipment.address_from.city", "San Francisco");
        json.updateString("batch_shipments[0].shipment.address_from.state", "CA");
        json.updateString("batch_shipments[0].shipment.address_from.zip", "94103");
        json.updateString("batch_shipments[0].shipment.address_from.country", "US");
        json.updateString("batch_shipments[0].shipment.address_from.phone", "4151234567");
        json.updateString("batch_shipments[0].shipment.address_from.email", "mrhippo@goshippo.com");
        json.updateString("batch_shipments[0].shipment.address_to.name", "Mrs Hippo");
        json.updateString("batch_shipments[0].shipment.address_to.company", "");
        json.updateString("batch_shipments[0].shipment.address_to.street1", "Broadway 1");
        json.updateString("batch_shipments[0].shipment.address_to.street2", "");
        json.updateString("batch_shipments[0].shipment.address_to.city", "New York");
        json.updateString("batch_shipments[0].shipment.address_to.state", "NY");
        json.updateString("batch_shipments[0].shipment.address_to.zip", "10007");
        json.updateString("batch_shipments[0].shipment.address_to.country", "US");
        json.updateString("batch_shipments[0].shipment.address_to.phone", "4151234567");
        json.updateString("batch_shipments[0].shipment.address_to.email", "mrshippo@goshippo.com");
        json.updateString("batch_shipments[0].shipment.parcels[0].length", "5");
        json.updateString("batch_shipments[0].shipment.parcels[0].width", "5");
        json.updateString("batch_shipments[0].shipment.parcels[0].height", "5");
        json.updateString("batch_shipments[0].shipment.parcels[0].distance_unit", "in");
        json.updateString("batch_shipments[0].shipment.parcels[0].weight", "2");
        json.updateString("batch_shipments[0].shipment.parcels[0].mass_unit", "oz");
        json.updateString("batch_shipments[1].shipment.address_from.name", "Mr Hippo");
        json.updateString("batch_shipments[1].shipment.address_from.street1", "1092 Indian Summer Ct");
        json.updateString("batch_shipments[1].shipment.address_from.city", "San Jose");
        json.updateString("batch_shipments[1].shipment.address_from.state", "CA");
        json.updateString("batch_shipments[1].shipment.address_from.zip", "95122");
        json.updateString("batch_shipments[1].shipment.address_from.country", "US");
        json.updateString("batch_shipments[1].shipment.address_from.phone", "4151234567");
        json.updateString("batch_shipments[1].shipment.address_from.email", "mrhippo@goshippo.com");
        json.updateString("batch_shipments[1].shipment.address_to.name", "Mrs Hippo");
        json.updateString("batch_shipments[1].shipment.address_to.company", "");
        json.updateString("batch_shipments[1].shipment.address_to.street1", "Broadway 1");
        json.updateString("batch_shipments[1].shipment.address_to.street2", "");
        json.updateString("batch_shipments[1].shipment.address_to.city", "New York");
        json.updateString("batch_shipments[1].shipment.address_to.state", "NY");
        json.updateString("batch_shipments[1].shipment.address_to.zip", "10007");
        json.updateString("batch_shipments[1].shipment.address_to.country", "US");
        json.updateString("batch_shipments[1].shipment.address_to.phone", "4151234567");
        json.updateString("batch_shipments[1].shipment.address_to.email", "mrshippo@goshippo.com");
        json.updateString("batch_shipments[1].shipment.parcels[0].length", "5");
        json.updateString("batch_shipments[1].shipment.parcels[0].width", "5");
        json.updateString("batch_shipments[1].shipment.parcels[0].height", "5");
        json.updateString("batch_shipments[1].shipment.parcels[0].distance_unit", "in");
        json.updateString("batch_shipments[1].shipment.parcels[0].weight", "20");
        json.updateString("batch_shipments[1].shipment.parcels[0].mass_unit", "lb");
        json.updateString("batch_shipments[1].carrier_account", "a4391cd4ab974f478f55dc08b5c8e3b3");
        json.updateString("batch_shipments[1].servicelevel_token", "fedex_2_day");

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

        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", "https://api.goshippo.com/batches/", 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)

        // {
        //   "object_id": "a015eb693cca465dbb6523ce6d2e3c65",
        //   "object_owner": "admin",
        //   "status": "VALIDATING",
        //   "object_created": "2016-09-12T15:25:43.465Z",
        //   "object_updated": "2016-09-12T15:25:43.465Z",
        //   "metadata": "BATCH #170",
        //   "default_carrier_account": "078870331023437cb917f5187429b093",
        //   "default_servicelevel_token": "usps_priority",
        //   "label_filetype": "PDF_4x6",
        //   "batch_shipments": {
        //     "next": null,
        //     "previous": null,
        //     "results": [
        //     ]
        //   },
        //   "object_results": {
        //     "purchase_succeeded": 0,
        //     "purchase_failed": 0,
        //     "creation_failed": 0,
        //     "creation_succeeded": 0
        //   },
        //   "label_url": [
        //   ]
        // }

        // 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_id = jResp.stringOf("object_id");
        String object_owner = jResp.stringOf("object_owner");
        String status = jResp.stringOf("status");
        String object_created = jResp.stringOf("object_created");
        String object_updated = jResp.stringOf("object_updated");
        String metadata = jResp.stringOf("metadata");
        String default_carrier_account = jResp.stringOf("default_carrier_account");
        String default_servicelevel_token = jResp.stringOf("default_servicelevel_token");
        String label_filetype = jResp.stringOf("label_filetype");
        String batch_shipmentsNext = jResp.stringOf("batch_shipments.next");
        String batch_shipmentsPrevious = jResp.stringOf("batch_shipments.previous");
        int object_resultsPurchase_succeeded = jResp.intOf("object_results.purchase_succeeded");
        int object_resultsPurchase_failed = jResp.intOf("object_results.purchase_failed");
        int object_resultsCreation_failed = jResp.intOf("object_results.creation_failed");
        int object_resultsCreation_succeeded = jResp.intOf("object_results.creation_succeeded");
        int i = 0;
        int count_i = jResp.sizeOfArray("batch_shipments.results");
        while (i < count_i) {
            jResp.setI(i);
            i = i + 1;
        }

        i = 0;
        count_i = jResp.sizeOfArray("label_url");
        while (i < count_i) {
            jResp.setI(i);
            i = i + 1;
        }
    }
}