How to Test APIs with cURL and JavaScript fetch()
Before you build anything with an API, you should test it first. Make sure it responds, check the data format, and verify that your parameters work. The two most common ways to test APIs are cURL (in your terminal) and fetch() (in JavaScript). Let's cover both.
Testing with cURL
cURL is a command-line tool that comes pre-installed on macOS and Linux (and can be installed on Windows). It lets you make HTTP requests directly from your terminal.
Basic GET request
This returns raw JSON in your terminal:
Pretty-print the response
Raw JSON is hard to read. Pipe it through python3 -m json.tool or jq for formatted output:
Or if you have jq installed:
Adding headers (for API keys)
Many APIs require an API key in the header:
Sending data with POST
See response headers
Use -i to include response headers (useful for checking rate limits):
This shows headers like X-RateLimit-Remaining before the response body.
Useful cURL flags
-s— Silent mode (hides progress bar)-i— Include response headers-X POST— Specify HTTP method-H "Header: Value"— Add a request header-d '{"json": "data"}'— Send request body-o file.json— Save response to a file
Testing with JavaScript fetch()
The fetch() function is built into every modern browser and Node.js 18+. You can run it in your browser's developer console (F12) or in a Node.js script.
Basic GET request
In the browser console, you can paste this directly. In Node.js, wrap it in an async function or use top-level await.
Checking the response status
Adding headers (for API keys)
POST request with JSON body
Error handling
Quick testing in your browser
The fastest way to test an API is right in your browser:
- Open any webpage
- Press F12 to open Developer Tools
- Click the Console tab
- Paste a fetch() call and press Enter
This works for any API that supports CORS. If you get a CORS error, you'll need to test from a server-side environment instead (Node.js or cURL).
cURL vs. fetch() — which to use?
- cURL: Best for quick one-off tests, checking headers, and APIs that don't support CORS. Works everywhere.
- fetch(): Best when you're already writing JavaScript and want to test in the same environment your app will use.
In practice, most developers use both. cURL for quick exploration, fetch() for integration testing. Pick whichever feels natural and test early, test often.