Adding a shipping address

How to add a shipping address to the order

Problem

You need to add a shipping address to a draft order. For example, you've added a billing address to an order but you don't want to use the same address for shipping.

Solution

To add a shipping address to an order, first you need to create the new address and then associate it with the order as its shipping address. To do that:

  1. Send a POST request to the /api/addresses endpoint.

  2. Send a PATCH request to the /api/orders/:id endpoint, setting its shipping_address relationship.

Example

1. Create a new address

The following request creates a new address:

curl -g -X POST \
  'https://yourdomain.commercelayer.io/api/addresses' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Content-Type: application/vnd.api+json' \
  -d'{
    "data": {
      "type": "addresses",
      "attributes": {
        "business": true,
        "company": "The Brand SRL",
        "line_1": "123 5th Avenue",
        "city": "New York",
        "zip_code": "10011",
        "state_code": "NY",
        "country_code": "US",
        "phone": "(212) 123-456-7890"
      }
    }
  }'

2. Associate the new address with the order

The following request associated the address identified by the "YWoeluJjEB" ID with the order identified by the "NgojhKoyYN" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/orders/NgojhKoyYN' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "orders",
      "id": "NgojhKoyYN",
      "relationships": {
        "shipping_address": {
          "data": {
            "type": "addresses",
            "id": "YWoeluJjEB"
          }
        }
      }
    }
  }'

Mapping

The image below shows how each field of a typical shipping address form is mapped to a specific field of the address or order objects.

Logged customers

When checking out an order as a logged customer — i.e. with a customer access token — you might want to give them the possibility to select a shipping address from their address book instead of creating a new address.

In this case, first you need to retrieve the list of the customer addresses, and then associate the selected one with the order.

To do that:

  1. Send a GET request to the /api/customer_addresses endpoint and include the associated addresses — the list is automatically filtered by the customer associated with the access token.

  2. Send a PATCH request to the /api/orders/:id endpoint, setting the selected address ID as the _shipping_address_clone_id.

Please note that the _shipping_address_clone_id is the ID of the selected address object, not the ID of the customer address object.

Example

1. Get the list of customer addresses

The following request retrieves the list of customer addresses:

curl -g -X GET \
  'https://yourdomain.commercelayer.io/api/customer_addresses?include=address' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json'

2. Associate the selected address with the order

The following request clones the address identified by the "YWoeluJjEB" ID and associates the newly created address with the order identified by the "NgojhKoyYN" ID as its shipping address:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/orders/NgojhKoyYN' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "orders",
      "id": "NgojhKoyYN",
      "attributes": {
        "_shipping_address_clone_id": "YWoeluJjEB"
      }
    }
  }'

Additional notes

Bill to the same address

When adding a shipping address to an order, you may want to use the same address for billing. To do that, send a PATCH request to the /api/orders/:id endpoint, setting the_billing_address_same_as_shipping attribute to true.

The following request clones the billing address of the order identified by the "NgojhKoyYN" ID and associates the newly created address with the order as its shipping address:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/orders/NgojhKoyYN' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "orders",
      "id": "NgojhKoyYN",
      "attributes": {
        "_billing_address_same_as_shipping": true
      }
    }
  }'

More to read

See our documentation if you need more information on how to create an address, list customer addresses, update an order, get a customer token, or include associations.

Last updated