Convert Curl to JavaScript

Convert curl commands to JavaScript fetch code instantly. Free online curl to JS converter that handles headers, authentication, and request bodies.

How to convert curl to JavaScript fetch

Copy a curl command from your terminal, API documentation, or browser DevTools.

  1. Paste the curl command into the input field above.
  2. Make sure "JavaScript" is selected as the target language.
  3. Click Convert. The generated Fetch API code appears on the right.
  4. Click Copy output and paste it into your project.

The converter runs entirely in your browser. No data is sent to any server, so you can safely paste commands containing API keys, tokens, or internal endpoints.

How curl flags map to fetch options

The Fetch API uses an options object as the second argument. Here is how curl flags translate:

Curl flag Fetch API equivalent
-X POST method: 'POST'
-H 'Key: Value' headers: { 'Key': 'Value' }
-d '{"key":"val"}' body: '{"key":"val"}'
-L (follow redirects) redirect: 'follow' (default)
-b 'cookie=val' Added to headers as Cookie

Browser vs Node.js usage

The generated code uses the Fetch API, which is available natively in all modern browsers and in Node.js 18+. If you are running Node.js 16 or earlier, you will need a polyfill like node-fetch.

In a browser context, the generated code works inside any async function or at the top level of an ES module. The await keyword pauses execution until the response arrives, then response.text() or response.json() gives you the body.

Converting to axios instead of fetch

This converter generates Fetch API code because it is the standard built into browsers and Node.js, so no dependencies are required. If your project uses axios, the generated code is still useful as a starting point: the headers object and body string are identical.

To adapt the output for axios, replace fetch(url, options) with axios({ url, ...options }) and change body to data. The rest carries over directly.

Handling authentication

Bearer tokens in curl (-H 'Authorization: Bearer token123') pass through as a regular header in the fetch options. The converter preserves the exact header value from the curl command.

For basic auth (-u user:pass), the converter adds a comment showing how to create the Authorization header using btoa(). The Fetch API does not have a built-in auth parameter like Python's requests library, so the base64 encoding step is necessary.

Cookies from -b are added to the headers object. In a browser, you may also want to set credentials: 'include' to send cookies from the browser's cookie jar.

Working with JSON responses

The generated code calls response.text() to read the response body as a string. If you know the API returns JSON, replace response.text() with response.json() to get a parsed JavaScript object directly.

For error handling, check response.ok before reading the body. The Fetch API does not throw on HTTP errors (4xx, 5xx). It only throws on network failures. Adding an if (!response.ok) check is recommended for production code.

Frequently Asked Questions

How do I convert curl to JavaScript?

Paste your curl command into the input field above, select "JavaScript," and click Convert. The output uses the Fetch API with async/await syntax and is ready to use in browsers and Node.js 18+.

Can I convert curl to fetch?

Yes. This tool generates Fetch API code directly. The output includes the method, headers, and body options mapped from the curl flags.

Does the generated code work with Node.js?

Yes. The Fetch API is available natively in Node.js 18 and later. For earlier versions, install the node-fetch package and add import fetch from 'node-fetch' at the top.

Can I use the output with axios instead?

The headers and body from the generated fetch code work with any JavaScript HTTP client. Replace fetch() with axios() and change body to data.

Is my curl command sent to a server?

No. All conversion happens in your browser. Nothing leaves your machine.

How do I handle authentication in fetch?

Bearer tokens are passed as a header (Authorization: Bearer ...). For basic auth, the converter shows how to use btoa() to encode the credentials. Both are included in the generated output when present in the curl command.

Related Free Tools

Give your AI agent a faster, leaner browser

Structured page data instead of raw HTML. Your agent processes less, decides faster, and costs less to run.

Stability detection built in
Fraction of the payload size
Diffs after every action
No credit card required. 1 hour of free runtime included.