Skip to content

Examples ​

These examples are based on an imaginary API. Jump here to see the resources it serves.

Activate customers ​

Activate all customers who are deactivated.

This example demonstrates:

  • setting default headers for all requests
    • in this example, an authorization header is set when creating the Api object
    • each request will include this header
  • getting the home resource
  • following a link to a collection resource
    • from home to customers
  • iterating over a collection
    • accessing the items in the collection using the collection() method ensures that each item is a HAL resource object
    • in other words, if you access the items directly, each item would not have the "has rel" method (for example)
  • checking if a resource has a specific link
  • using a resource's affordance to change its state (using PUT to activate a customer)
    • contrast this with setting the active property directly
    • by using the affordance, the API can enforce business rules, and it can change how activation occur without changing the client code

Print the orders for a given customer.

This example demonstrates:

  • following a link to a resource with a templated URL
  • using the response status code to navigate the user's workflow
  • following a link from one resource to its child resource collection
  • mixing direct access to _items with managed iteration using the collection() method

Pagination ​

Pagination using query string parameters.

This example demonstrates:

  • using query string parameters to do pagination

Sample API ​

The examples above are based on an imaginary API that serves the following resources:

/
/customers
{
  "_links": {
    "self: {
      "href": "/"
    },
    "customers": {
      "href": "/customers"
    },
    "orders": {
      "href": "/orders"
    }
  }
}
{
  "_items": [
    {
      "membershipId": "A375",
      "givenName": "Pat",
      "familyName": "Smith",
      "active": true,
      "_links": {
        "self": {
          "href": "/customers/A375",
        },
        "edit-form": {
          "href": "/customers/A375/edit-form",
        },
        "orders": {
          "href": "/customers/A375/orders",
        },
        "deactivate": {
          "href": "/customers/A375/deactivate",
          "_note": "PUT to deactivate"
        }
      }
    },
    {
      "membershipId": "R933",
      "givenName": "Darcy",
      "familyName": "Jones",
      "active": false,
      "_links": {
        "self": {
          "href": "/customers/R933",
        },
        "edit-form": {
          "href": "/customers/R933/edit-form",
        },
        "orders": {
          "href": "/customers/R933/orders",
        },
        "activate": {
          "href": "/customers/R933/activate",
          "_note": "PUT to activate"
        }
      }
    },
    ...
    ...
    ...
  ],
  "_links": {
    "self": {
      "href": "/customers"
    },
    "item": {
      "href": "/customers/{membershipId}",
      "templated": true
    },
    "create-form": {
      "href": "/customers/create-form",
    }
  }
}
/customers/A375
/customers/A375/orders
{
  "membershipId": "A375",
  "givenName": "Pat",
  "familyName": "Smith",
  "active": true,
  "_links": {
    "self": {
      "href": "/customers/A375",
    },
    "edit-form": {
      "href": "/customers/A375/edit-form",
    },
    "orders": {
      "href": "/customers/A375/orders",
    },
    "deactivate": {
      "href": "/customers/A375/deactivate",
      "_note": "PUT to deactivate this member"
    }
  }
}
{
  "_items": [
    {
      "orderNumber": "FK9384",
      "partNumber": "009343-12",
      "quantity": 3,
      "_customer_ref": "A375",
      "_links": {
        "self": {
          "href": "/orders/65fd80549561b2884948c312",
        },
        "edit-form": {
          "href": "/orders/65fd80549561b2884948c312/edit-form",
        },
        "parent": {
          "href": "/customers/A375",
        },
        "collection": {
          "href": "/customers/A375/orders"
        }
      }
    },
    ...
    ...
    ...
  ],
  "_links": {
    "self": {
      "href": "/customers/A375/orders"
    },
    "item": {
      "href": "/orders/{id}",
      "templated": true
    },
    "create-form": {
      "href": "/customers/A375/create-form",
    }
  }
}

Released under the MIT License.