← All tools

cURL to fetch

Turn any curl command into fetch() for the browser or Node 18+.
Try
POSThttps://api.example.com/v1/customers
HEADERS · 2
AuthorizationBearer sk_t••••••→ API_TOKEN
Content-Typeapplication/json
BODY · JSON
{
  "email": "ada@example.com",
  "name": "Ada Lovelace",
  "metadata": {
    "plan": "pro",
    "seats": 3
  }
}
fetchBrowsers and Node 18+
const res = await fetch("https://api.example.com/v1/customers", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.API_TOKEN}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    email: "ada@example.com",
    name: "Ada Lovelace",
    metadata: {
      plan: "pro",
      seats: 3,
    },
  }),
});

if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
18 linesReads secrets from process.env
● Parsed in this tab-X -H -d --json -F -u -b -G -L -k -m and moreTokens and keys are never sent

Things that change in fetch

curl and the browser don't behave the same.

  • CookiesBrowsers won't let fetch set a Cookie header. Use credentials: "include" and let the browser send its own cookies.
  • CORScurl ignores CORS; the browser doesn't. If the API doesn't allow your origin, call it from your server instead.
  • Redirectsfetch follows redirects by default. curl only does with -L.
  • Timeoutscurl's -m becomes signal: AbortSignal.timeout(ms). Without it, fetch waits as long as the browser does.

Questions

The short answers.

Does this work in Node?

Yes. fetch is built into Node 18 and newer, with the same code.

Why is there no Content-Type for file uploads?

With FormData the browser sets it for you, including the boundary. Setting it yourself breaks the upload.

Why doesn't fetch throw on a 404?

fetch only rejects on network errors. The code checks res.ok and throws for 4xx and 5xx so errors don't slip through.

Are large numbers in a JSON body kept?

Yes. If a number is too big for a JavaScript number, like a 20-digit ID, the body is sent as the original JSON text instead of being rebuilt, so no digits are lost.

Where do tokens and cookies go?

With “Secrets as env variables” on, they're read from process.env, so they stay out of your code and out of anything you paste it into.

See all →