How to convert curl to JavaScript
- Paste your curl command (from API docs, a terminal or your browser's dev tools).
- The JavaScript code appears instantly, using fetch. Switch tabs to see other languages.
- Copy the code and run it; no extra packages are needed.
How curl options map to fetch
| curl | JavaScript |
|---|---|
-X POST | method: "POST" |
-H 'Name: value' | headers: {...} |
-d '{"json": true}' | body: JSON.stringify({...}) |
-F 'name=value' | body: FormData |
-u user:pass | Authorization: "Basic " + btoa("user:pass") |
-m 10 | signal: AbortSignal.timeout(10000) |
-k | Not possible in fetch (fix the certificate) |
Tips for the generated JavaScript code
- fetch doesn't throw on 4xx or 5xx responses. Check response.ok (or response.status) before using the body.
- In the browser, the API must allow your site with CORS headers. If curl works but fetch fails with a CORS error, call the API from your server instead.
- Never put secret API keys in browser code; anyone can read them. Call keyed APIs from a server or serverless function.
Convert curl to other languages
The same converter writes Python, Node.js (axios), Go, PHP and Ruby. See the cURL converter for every supported option.
Need an API to try it on? Every API in our list of APIs with no key works with a plain curl command.