cURL to fetch is a free converter that turns cURL commands into JavaScript code using the built-infetch API. Headers, JSON bodies, form data, cookies and basic authentication are all carried over. The command is converted in your browser, so tokens and passwords in it stay private.
How to convert cURL to fetch
- Paste a cURL command into the left box.
- The JavaScript appears on the right. Switch to Python or axios with the tabs if you need another language.
- Copy the code into your project. It uses
await, so run it inside an async function or a module.
Getting a cURL command
- From your browser: open developer tools (F12), go to the Network tab, right-click a request and choose Copy → Copy as cURL. On Windows, pick the “bash” version.
- From API documentation: most APIs (Stripe, GitHub, OpenAI and others) show examples as cURL commands.
- From Postman or Insomnia: use the code snippet or “Copy as cURL” option.
What the generated code does
- JSON bodies become
JSON.stringify(…)with a readable object, so they are easy to edit. - -u user:pass becomes an
Authorization: Basicheader built withbtoa. - -F form fields become a
FormDataobject; file fields use a file input. - No -L adds
redirect: 'manual', because fetch follows redirects and curl doesn’t by default.
Running it in a browser vs Node.js
fetch is built into browsers and into Node.js 18 and later, so the same code works in both. In a browser, the API must allow your site through CORS, and headers such as Cookie andUser-Agent can’t be set by scripts. For server-side code you may prefercURL to axios.
Worked example: posting a todo as JSON
Paste this command:
curl -X POST 'https://api.example.com/v1/todos' \
-H 'Authorization: Bearer abc123' \
--json '{"title":"Buy milk","done":false}'You get back:
const response = await fetch('https://api.example.com/v1/todos', {
method: 'POST',
headers: {
'Authorization': 'Bearer abc123',
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
"title": "Buy milk",
"done": false
}),
redirect: 'manual', // curl doesn't follow redirects without -L
});
const data = await response.json();The --json flag grows both content headers, and the body is a readable object insideJSON.stringify so it is easy to edit before sending.
Worked example: basic auth in one header
Paste this command:
curl -u api:key123 'https://api.example.com/whoami'You get back:
const response = await fetch('https://api.example.com/whoami', {
headers: {
'Authorization': 'Basic ' + btoa('api:key123'),
},
redirect: 'manual', // curl doesn't follow redirects without -L
});
const text = await response.text();-u becomes a Basic header assembled with btoa. Replace the embedded credentials with environment variables before the code leaves your machine.
When the code will not run in a browser
- Authorization triggers a preflight. Any request with an
Authorizationheader makes the browser send an OPTIONS check first. If the API does not answer it with your origin allowed, the call fails before it starts — the same code works untouched in Node.js. - Some headers are unsettable.
Cookie,User-AgentandRefererare forbidden to scripts; the browser silently drops them. Converted code carrying them only behaves fully on the server side. - File fields need a file picker. A converted
form.append('file', fileInput.files[0])expectsconst fileInput = document.getElementById('upload')wired to an<input type="file" id="upload">element — add both, or the field sends nothing.
Frequently asked questions
Is it safe to paste commands with tokens or passwords?
Yes. The command is converted inside your browser and never sent anywhere. Still, remember to remove real secrets before sharing the generated code with others.
Which cURL options are supported?
The ones used for API requests: -X, -H, -d and its variants, --json, -F (forms and file uploads), -u (basic auth), -b (cookies), -A, -e, -G, -I, -L, -k and --compressed. Output options such as -o, -s and -v are ignored, and anything else is listed as a warning.
Why does the code include redirect: 'manual'?
Without -L, curl doesn’t follow redirects, but fetch does by default. The option keeps the behaviour the same; delete it if you do want redirects followed.
Will this work in the browser?
Yes, if the API allows it. Browsers enforce CORS: the API must allow requests from your site. Some headers, such as Cookie and User-Agent, can’t be set from browser JavaScript and are ignored there, but they work in Node.js.
Last updated