Sample code for 30+ languages & platforms
Swift

Shippo Create an Order

See more Shippo Examples

Demonstrates how to create an order with all the information about your order by sending a POST request to the orders endpoint.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let http = CkoHttp()!

    // Implements the following CURL command:

    // curl https://api.goshippo.com/orders/ \
    //     -H "Authorization: ShippoToken <API_Token>" \
    //     -H "Content-Type: application/json"  \
    //     -d '{
    //             "to_address": {
    //                 "city": "San Francisco",
    //                 "company": "Shippo",
    //                 "country": "US",
    //                 "email": "shippotle@goshippo.com",
    //                 "name": "Mr Hippo",
    //                 "phone": "15553419393",
    //                 "state": "CA",
    //                 "street1": "215 Clayton St.",
    //                 "zip": "94117"
    //             },
    //             "line_items": [
    //                 {
    //                     "quantity": 1,
    //                     "sku": "HM-123",
    //                     "title": "Hippo Magazines",
    //                     "total_price": "12.10",
    //                     "currency": "USD",
    //                     "weight": "0.40",
    //                     "weight_unit": "lb"
    //                 }
    //             ],
    //             "placed_at": "2016-09-23T01:28:12Z",
    //             "order_number": "#1068",
    //             "order_status": "PAID",
    //             "shipping_cost": "12.83",
    //             "shipping_cost_currency": "USD",
    //             "shipping_method": "USPS First Class Package",
    //             "subtotal_price": "12.10",
    //             "total_price": "24.93",
    //             "total_tax": "0.00",
    //             "currency": "USD",
    //             "weight": "0.40",
    //             "weight_unit": "lb"
    //         }'

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

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

    // {
    //   "to_address": {
    //     "city": "San Francisco",
    //     "company": "Shippo",
    //     "country": "US",
    //     "email": "shippotle@goshippo.com",
    //     "name": "Mr Hippo",
    //     "phone": "15553419393",
    //     "state": "CA",
    //     "street1": "215 Clayton St.",
    //     "zip": "94117"
    //   },
    //   "line_items": [
    //     {
    //       "quantity": 1,
    //       "sku": "HM-123",
    //       "title": "Hippo Magazines",
    //       "total_price": "12.10",
    //       "currency": "USD",
    //       "weight": "0.40",
    //       "weight_unit": "lb"
    //     }
    //   ],
    //   "placed_at": "2016-09-23T01:28:12Z",
    //   "order_number": "#1068",
    //   "order_status": "PAID",
    //   "shipping_cost": "12.83",
    //   "shipping_cost_currency": "USD",
    //   "shipping_method": "USPS First Class Package",
    //   "subtotal_price": "12.10",
    //   "total_price": "24.93",
    //   "total_tax": "0.00",
    //   "currency": "USD",
    //   "weight": "0.40",
    //   "weight_unit": "lb"
    // }

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "to_address.city", value: "San Francisco")
    json.updateString(jsonPath: "to_address.company", value: "Shippo")
    json.updateString(jsonPath: "to_address.country", value: "US")
    json.updateString(jsonPath: "to_address.email", value: "shippotle@goshippo.com")
    json.updateString(jsonPath: "to_address.name", value: "Mr Hippo")
    json.updateString(jsonPath: "to_address.phone", value: "15553419393")
    json.updateString(jsonPath: "to_address.state", value: "CA")
    json.updateString(jsonPath: "to_address.street1", value: "215 Clayton St.")
    json.updateString(jsonPath: "to_address.zip", value: "94117")
    json.updateInt(jsonPath: "line_items[0].quantity", value: 1)
    json.updateString(jsonPath: "line_items[0].sku", value: "HM-123")
    json.updateString(jsonPath: "line_items[0].title", value: "Hippo Magazines")
    json.updateString(jsonPath: "line_items[0].total_price", value: "12.10")
    json.updateString(jsonPath: "line_items[0].currency", value: "USD")
    json.updateString(jsonPath: "line_items[0].weight", value: "0.40")
    json.updateString(jsonPath: "line_items[0].weight_unit", value: "lb")
    json.updateString(jsonPath: "placed_at", value: "2016-09-23T01:28:12Z")
    json.updateString(jsonPath: "order_number", value: "#1068")
    json.updateString(jsonPath: "order_status", value: "PAID")
    json.updateString(jsonPath: "shipping_cost", value: "12.83")
    json.updateString(jsonPath: "shipping_cost_currency", value: "USD")
    json.updateString(jsonPath: "shipping_method", value: "USPS First Class Package")
    json.updateString(jsonPath: "subtotal_price", value: "12.10")
    json.updateString(jsonPath: "total_price", value: "24.93")
    json.updateString(jsonPath: "total_tax", value: "0.00")
    json.updateString(jsonPath: "currency", value: "USD")
    json.updateString(jsonPath: "weight", value: "0.40")
    json.updateString(jsonPath: "weight_unit", value: "lb")

    http.setRequestHeader(name: "Authorization", value: "ShippoToken <API_Token>")
    http.setRequestHeader(name: "Content-Type", value: "application/json")

    let resp = CkoHttpResponse()!
    success = http.httpJson(verb: "POST", url: "https://api.goshippo.com/orders/", json: json, contentType: "application/json", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    let sbResponseBody = CkoStringBuilder()!
    resp.getBodySb(sb: sbResponseBody)
    let jResp = CkoJsonObject()!
    jResp.loadSb(sb: sbResponseBody)
    jResp.emitCompact = false

    print("Response Body:")
    print("\(jResp.emit()!)")

    var respStatusCode: Int = resp.statusCode.intValue
    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)

    // {}

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


}