Chris Balthazard

API Endpoints

I’ve accessed data via APIs fairly frequently, but have not dived into the details of how those calls work. So let’s do that here.

APIs (Application Programming Interface) can generally be SOAP or REST (REpresentational State Transfer) - let’s focus on REST. There’s plenty to say about when and why to use APIs in making data available, but I want to look more at how to use them. What data needs to be sent (this article), and how do we structure that in Ruby (the next article)?

Methods

These are your typical CRUD (create, read, update, delete) methods: PUT, POST, GET, and DELETE. We’ll ignore PATCH. DELETE is a delete, GET is a read (and does not include a request body).

PUT either creates or updates a resource at a particular URI (Uniform Resource Identifier), specified in the request body (it refers to a singleton resource). It is idempotent, so you can run the same PUT multiple times and the requests after the first one will not change anything.

POST either creates or updates a resource for a collection URI, and is not idempotent. In practice, POST is usually used as a create, and PUT as an update.

Parameters

Options that can be passed with the request which may change the result. These are analogous to url params. They are often filters; pagination can also be implemented here with some combination of offset, limit, page, and page number.

HTTP (request) headers

Key-value pairs that are sent along with the request. More metadata than parameters; however, headers, parameters, and body can flow together a bit. Some common examples:

Request body

The data used in creating, updating, or deleting a resource. Can also be used to narrow down results, functioning similar to parameters. If you’re using an API to add a blog post, the text of the post would go here.

Sources/further reading