Adding a billing address

How to add a billing address to the order

Problem

You need to add a billing address to a draft order, for example as the first step of the order checkout flow.

Solution

To add a billing address to an order, you first need to create the new address and then associate it with the order as its billing 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 billing_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": {
        "first_name": "John",
        "last_name": "Doe",
        "line_1": "456 Steinway Street",
        "city": "New York",
        "zip_code": "11103",
        "state_code": "NY",
        "country_code": "US",
        "phone": "(347) 321-654-0987"
      }
    }
  }'

2. Associate the new address with the order

The following request associates the address identified by the "BgnguJvXmb" 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": {
        "billing_address": {
          "data": {
            "type": "addresses",
            "id": "BgnguJvXmb"
          }
        }
      }
    }
  }'

Mapping

The image below shows how each of the fields of a typical billing 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 billing address from their address book instead of creating a new address.

In this case, you first 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 _billing_address_clone_id.

Please note that the _billing_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 "BgnguJvXmb" ID and associates the newly created address with the order identified by the "NgojhKoyYN" ID as its billing 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_clone_id": "BgnguJvXmb"
      }
    }
  }'

Additional notes

Ship to the same address

When adding a billing address to an order, a common use case is to use the same address for shipping. To do that, send a PATCH request to the /api/orders/:id endpoint, setting the_shipping_address_same_as_billing 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": {
        "_shipping_address_same_as_billing": 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