Making Peace With HTTP APIs

| Comments

Once in a while you come across an HTTP API that uses HTTP in complicated and incorrect ways. There are many examples of this on the Web today including those from eBay, Amazon, Google, Microsoft and many many others. These can be hard to use as they require you to follow proprietary styles for constructing requests and parsing responses. Some of those also don’t work well with common HTTP infrastructure like caches.

In this post, I would like to show how you can, in four simple steps, use to hide the complexity of such APIs.

To illustrate, let me take eBay’s PlaceOffer API that lets an eBay buyer place an offer for an item listed on eBay. This example may be more complex than other similar APIs that you have encoutered, but it helps me drive the point.

This API requires you to send a POST request with some custom headers and an XML document in the body.

POST /ws/api.dll HTTP/1.1
Content-Type: application/xml; charset=UTF-8

See developer docs for more details of these headers.

The body of the request is an XML document. An example is below.

<PlaceOfferRequest xmlns="urn:ebay:apis:eBLBaseComponents">
    <MaxBid currencyID="USD">20.00</MaxBid>

A response from this API looks like the following:

<PlaceOfferResponse xmlns="urn:ebay:apis:eBLBaseComponents">
    <ConvertedCurrentPrice currencyID="USD">1.0</ConvertedCurrentPrice>
    <CurrentPrice currencyID="USD">1.0</CurrentPrice>
    <MinimumToBid currencyID="USD">1.25</MinimumToBid>

Here is how you can use to simplify this.

Step 0: Create an App

Create a app.

mkdir myapp
cd myapp
curl | bash

See docs for more details.

Step 1: Create a Table

Place the following in tables/placeoffer.ql.

This step binds the API into the runtime so that you can use’s DSL to send requests and process responses.

Step 2: Describe the Shape of the Request Body

Place the following in tables/

This is just a mustache template. You can use EJS too if you like.

Step 3: Create a Route

Place the following in routes/placeoffer.ql

Step 4: Use the API

POST /offers?siteId=0&itemId=your-item-id&offer=your-offer&action=your-action&quantity=your-quantity
Authorization: your auth token

This request returns JSON.

Step 5: Enjoy

No XML, no schemas, no SDKs. As an added benefit, you can combine this API with other APIs as you like using’s DSL.

Why does this matter? If you have a legacy API that you can not afford to rewrite to use HTTP sanely, can help you hide it behind a saner interface.

Thanks to Juan Rodriguez for showing me this example.