API Bouncer

Buy me a coffee

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

curl https://dog.ceo/api/breeds/image/random

This returns raw JSON in your terminal:

{"message":"https://images.dog.ceo/breeds/terrier-norwich/n02094258_1003.jpg","status":"success"}

Pretty-print the response

Raw JSON is hard to read. Pipe it through python3 -m json.tool or jq for formatted output:

curl -s https://dog.ceo/api/breeds/image/random | python3 -m json.tool

Or if you have jq installed:

curl -s https://dog.ceo/api/breeds/image/random | jq .

Adding headers (for API keys)

Many APIs require an API key in the header:

curl -H "Authorization: Bearer YOUR_API_KEY" https://api.example.com/data

Sending data with POST

curl -X POST https://api.example.com/items \ -H "Content-Type: application/json" \ -d '{"name": "My Item", "value": 42}'

See response headers

Use -i to include response headers (useful for checking rate limits):

curl -i https://api.example.com/data

This shows headers like X-RateLimit-Remaining before the response body.

Useful cURL flags

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

const response = await fetch('https://dog.ceo/api/breeds/image/random'); const data = await response.json(); console.log(data);

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

const response = await fetch('https://api.example.com/data'); console.log(response.status); // 200, 404, 429, etc. console.log(response.ok); // true if status is 200-299 console.log(response.statusText); // "OK", "Not Found", etc.

Adding headers (for API keys)

const response = await fetch('https://api.example.com/data', { headers: { 'Authorization': 'Bearer YOUR_API_KEY', 'Accept': 'application/json' } }); const data = await response.json();

POST request with JSON body

const response = await fetch('https://api.example.com/items', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'My Item', value: 42 }) }); const result = await response.json();

Error handling

try { const response = await fetch('https://api.example.com/data'); if (!response.ok) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); } const data = await response.json(); console.log(data); } catch (error) { console.error('API request failed:', error.message); }

Quick testing in your browser

The fastest way to test an API is right in your browser:

  1. Open any webpage
  2. Press F12 to open Developer Tools
  3. Click the Console tab
  4. 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?

In practice, most developers use both. cURL for quick exploration, fetch() for integration testing. Pick whichever feels natural and test early, test often.