Automate Workflows with n8n and APIs
Understanding HTTP and APIs
How Websites Talk to Each Other
Think of an API, or Application Programming Interface, as a waiter in a restaurant. You, the customer, don't go directly into the kitchen to tell the chef what you want. Instead, you give your order to the waiter. The waiter takes your request to the kitchen, gets the food, and brings it back to you. The waiter is the intermediary that lets you communicate with the kitchen without needing to know how it works.
In the digital world, applications do the same thing. An API allows one piece of software to make requests to another and get a response. The language they use to communicate is called HTTP.
API
noun
An Application Programming Interface is a set of rules and protocols that allows different software applications to communicate with each other.
HTTP stands for Hypertext Transfer Protocol. It's the foundation of data communication for the World Wide Web. Every time you load a webpage, your browser sends an HTTP request to a server, and the server sends an HTTP response back. This conversation is known as the request-response cycle.
The Request-Response Cycle
An HTTP request has a few key parts that tell the server exactly what the client wants.
Here's what each part does:
- Method: The action you want to perform.
GETretrieves data,POSTcreates new data,PUTupdates existing data, andDELETEremoves data. - URL: The address of the resource you want to interact with on the server.
- Headers: Extra information for the server, like the type of content you're sending or authentication details.
- Body: The actual data you're sending to the server, often used with
POSTorPUTrequests.GETrequests usually don't have a body.
After the server receives the request, it processes it and sends back an HTTP response.
The response also has a few parts:
- Status Code & Message: A number and a short message indicating the result of the request. Was it successful? Was there an error?
- Headers: Extra information about the response, like the type of content being sent back.
- Body: The content the server is sending back, which could be HTML, JSON data, an image, or something else.
Understanding Server Replies
Status codes are a quick way to understand what happened with your request. They are grouped into categories based on the first digit. For instance, codes in the 200s mean success, while codes in the 400s indicate a problem with your request.
| Status Code | Meaning | Analogy |
|---|---|---|
| 200 OK | The request succeeded. | Your order was successful. Here is your food. |
| 404 Not Found | The server can't find the requested resource. | We don't have that dish on the menu. |
| 401 Unauthorized | You aren't authenticated. | You need to show your ID to enter this club. |
| 403 Forbidden | You are authenticated, but don't have permission. | Your ID is valid, but this is a VIP area. |
| 500 Internal Server Error | Something went wrong on the server's end. | The kitchen had an unexpected problem. |
Knowing these common codes helps you quickly diagnose issues when working with APIs.
Gaining Access with Keys
Many APIs are not open to the public. They require you to identify yourself before you can use them. This process is called authentication, and it ensures that only authorized users can access the data.
One of the most common ways to authenticate is with an API key. An API key is a unique string of characters that you include in your request, usually in the headers. It's like a secret password that proves you have permission to use the service.
Always keep your API keys private. If someone else gets your key, they could make requests on your behalf, which might cost you money or compromise your data.
Another simple method is Basic Authentication, which uses a username and password. The key takeaway is that you almost always need some form of credentials to use a private API.
A Style for APIs: REST
You'll often hear the term "REST API." REST, which stands for Representational State Transfer, isn't a strict protocol but a set of architectural guidelines for building web services. APIs that follow these guidelines are called RESTful.
RESTful APIs are popular because they are simple, scalable, and rely on the standard HTTP methods we've already discussed. The core idea is to treat everything as a "resource." A resource could be a user, a product, a photo, or anything else.
Here's how REST uses HTTP methods to perform actions on resources, often referred to by the acronym CRUD (Create, Read, Update, Delete).
| Action | HTTP Method | Example URL | Description |
|---|---|---|---|
| Create | POST | /users | Creates a new user. |
| Read | GET | /users/123 | Retrieves details for user 123. |
| Update | PUT | /users/123 | Updates the details for user 123. |
| Delete | DELETE | /users/123 | Deletes user 123. |
This predictable structure makes RESTful APIs easy to understand and use. When you know an API is RESTful, you can make good guesses about how to interact with it just by knowing the resource URLs and standard HTTP methods.
Now that you know the basics, let's see what you've learned.
In the restaurant analogy, what role does the API play?
If you wanted to retrieve a list of products from an online store's API, which HTTP method would you most likely use?
Understanding how APIs and HTTP work together is the first step to connecting different services and automating tasks on the web.