Automate the busywork

Test an API with curl in Five Minutes

curl is a command-line tool that sends web requests, and it is the fastest way to find out whether an API does what its documentation says. Type a command, read the response, and you know. It is already installed on macOS and most Linux systems, and recent versions of Windows include it too.

Testing with curl before you build an automation saves time. If a request works in the terminal, you know the address, the credentials and the data are right, and any remaining trouble lies in your workflow tool, not the API.

Your first request

Open a terminal and type curl followed by an address. For example, curl https://example.com fetches that page and prints it. Pointed at an API address, it prints the data.

By default curl sends a GET request, which reads data. Try it on a public API that needs no key, such as a service that returns a random joke or the current time. You will see JSON appear in the terminal.

If the output is one long line, it is hard to read. Piping the result into a JSON formatter such as jq makes it tidy. Many systems have it available, or you can paste the text into an online viewer.

Seeing more with the verbose flag

Add -i to include the response headers, or -v for verbose output that shows both your request and the reply. These flags reveal the status code, the content type and any rate limit headers.

When something fails, verbose mode is the first thing to try. It shows what curl actually sent, which often differs from what you thought you sent.

A quieter option is -s, which hides the progress meter. Combine it with -w "%{http_code}" to print just the status code after the response.

Sending headers and a key

Most APIs need credentials. You send them as headers with the -H option. A common pattern is an authorization header carrying a token, written as -H "Authorization: Bearer YOUR_TOKEN". Other services use their own header name for a key, so check the documentation.

Do not type your real key straight into a command if you can avoid it. Commands are saved in your shell history. Put the key in an environment variable and refer to it in the command instead, so the secret never appears in the text you type.

If you get a 401 or 403 response, the problem is almost always the credentials: a typo, a missing prefix, an expired token or a key without the right permission.

Sending data with POST

To create something, use -X POST, add a content type header and supply the body with -d. The body is usually JSON, so the header says Content-Type: application/json, and the data goes in single quotes after -d.

For example, a request to create a contact might send a name and an email in a small JSON object. The API replies with the new record, including an ID.

For longer bodies, save the JSON in a file and send it with -d @file.json. That avoids quoting headaches, because special characters in a typed command can be altered by your shell.

Remember that POST usually creates a record every time you run it. Test against a sandbox or a test account when you can, so you do not litter real data.

Updating and deleting

The same pattern works for other methods. Use -X PATCH or -X PUT with a body to update a record, and -X DELETE to remove one. Include the record’s ID in the address.

Be careful with DELETE. Run a GET on the same address first to confirm you are about to remove the right record, then run the delete.

Reading errors

When a request fails, read three things in order: the status code, the response body and the headers.

A 400 means something is wrong with your request, and the body often names the field. A 404 means the address or ID is wrong, so check for typos and for the right base address. A 429 means you are being rate limited, and the headers may tell you how long to wait. A 500 means the server has trouble, and trying again later may help.

If the response is blank, add -v to see whether the connection even succeeded, or whether a redirect sent you elsewhere. The -L option tells curl to follow redirects.

Handy options worth remembering

A few more flags come up often. -o filename saves the response to a file instead of printing it, which is useful for large results. --data-urlencode safely sends values containing spaces or symbols as form data. -u user:password sends basic authentication for older services that use it. And --max-time 10 stops a request that hangs, so a stuck connection does not freeze your terminal.

Build commands up gradually. Start with the simplest working request, confirm it returns a 200, and then add one option at a time. When a change breaks it, you know exactly which one did.

Turning a working command into an automation

Once a command works, you have the pieces any tool needs: the address, the method, the headers and the body. Copy them into your workflow tool’s HTTP step field by field. Many tools can even import a curl command and fill the form for you.

Keep the working command in a note beside your automation. When something breaks months later, running it again shows right away whether the API changed or the problem lies elsewhere.

Your next step

Choose one API you plan to use, and find its first example request in the documentation. Run it with curl and a test key. Confirm you get a 200 response and can find a field you care about.

Then try one deliberate failure, such as a wrong key, and note the status code and message. Knowing what a failure looks like is half of building a reliable automation.