Convert cURL to JavaScript fetch

Paste a curl command to get fetch() code that works in the browser, Deno and Bun. Pick Node.js in the converter for server-side code.

AI DevRuns in your browser — nothing uploaded

Output language

Converted in your browser — tokens and passwords in the command never leave your device. Tip: in Chrome DevTools, right-click a request in the Network tab → Copy → Copy as cURL.

How curl options map to fetch()

fetch is built into every browser, so there’s nothing to install — but it has fewer options than curl, and some things have to be written out by hand.

  1. Headers go in the headers object. fetch doesn’t add a Content-Type for a string body, so the converter keeps the one from the command.
  2. A JSON body becomes body: JSON.stringify({...}) — fetch has no json option.
  3. -d form fields become new URLSearchParams({...}), and fetch sets the urlencoded Content-Type for you.
  4. -F becomes a FormData object. Don’t set Content-Type yourself: fetch adds it with the multipart boundary.
  5. -u user:pass becomes an Authorization: Basic … header, already base64-encoded, because fetch has no auth option.
  6. --max-time becomes signal: AbortSignal.timeout(ms).

A request that works in curl can still fail in the browser. The API has to allow your site’s origin (CORS), and browsers don’t let scripts set some headers, such as Cookie and Host. For server-side code, choose Node.js in the converter instead.

Example: curl to JavaScript fetch

This command, pasted into the converter above, gives the code below.

cURL command
curl 'https://api.example.com/v1/projects?page=2&per_page=50' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer YOUR_API_KEY'
JavaScript fetch
const response = await fetch("https://api.example.com/v1/projects?page=2&per_page=50", {
  headers: {
    Accept: "application/json",
    Authorization: "Bearer YOUR_API_KEY",
  },
});

console.log(response.status);
console.log(await response.text());

How it works

  1. 1

    Reads the command like a shell

    Single and double quotes, $'…' strings, backslash line breaks and Windows ^ escapes are all understood — so Chrome’s “Copy as cURL (bash)” and “(cmd)” both work.

  2. 2

    Understands curl’s options

    -X, -H, -d, --data-raw, --data-urlencode, --json, -F, -u, -b, -G, -k, -L, -x, --max-time and more — with curl’s own defaults, like POST when there’s a body.

  3. 3

    Writes idiomatic code

    json= in Python, URLSearchParams and FormData in JavaScript, a multipart writer in Go, CURLFile in PHP and a text block in Java — the way each language would normally do it, using only what ships with it where possible.

  4. 4

    Tested against real curl

    Code in all seven languages was run against a test server and checked to send the same method, URL, headers and body as curl itself.

Questions, answered

curl isn’t a browser, so CORS doesn’t apply to it. In a browser, the API must send an Access-Control-Allow-Origin header that allows your site. If it doesn’t, call the API from your own server (for example with the Node.js output) and fetch from there.

Popular uses

Read the guide