cURL to axios is a free converter that turns cURL commands into Node.js code using axios. Headers, JSON data, URL-encoded forms, multipart uploads, basic auth and TLS settings are converted. It runs in your browser, so secrets in the command stay on your device.
How to convert cURL to axios
- Paste the cURL command.
- Copy the generated code.
- Install axios with
npm install axiosand run it with Node.js (as an ES module, e.g. a.mjsfile).
From API docs to a Node.js service
API providers usually document their endpoints with cURL examples. Converting them to axios gives you a starting point for a Node.js backend, script or serverless function. Move tokens such as Bearer keys into environment variables (process.env.API_KEY) before committing the code.
Worked example: posting JSON
Paste this command:
curl -X POST 'https://api.example.com/v1/orders' \
-H 'Authorization: Bearer abc123' \
--json '{"item":"Mug","quantity":2}'You get back:
import axios from 'axios';
const response = await axios({
method: 'post',
url: 'https://api.example.com/v1/orders',
headers: {
'Authorization': 'Bearer abc123',
'Content-Type': 'application/json',
'Accept': 'application/json',
},
data: {
"item": "Mug",
"quantity": 2
},
maxRedirects: 0,
});
console.log(response.data);Notice that the body is a plain JavaScript object: axios serialises it itself and setsContent-Type, so there is no JSON.stringify to forget.
Worked example: basic authentication
Paste this command:
curl -u api:key123 'https://api.example.com/whoami'You get back:
import axios from 'axios';
const response = await axios({
method: 'get',
url: 'https://api.example.com/whoami',
auth: { username: 'api', password: 'key123' },
maxRedirects: 0,
});
console.log(response.data);-u maps onto axios’s own auth option, which builds the Basic header for you — cleaner than assembling btoa by hand, and it works the same way in Node.js where btoa may not exist.
How options are translated
- JSON bodies are passed as a JavaScript object in
data; axios serialises it and sets the Content-Type. - URL-encoded forms (
-d a=1&b=2) useURLSearchParams. - -u uses axios’s
authoption. - No -L sets
maxRedirects: 0, matching curl’s default of not following redirects. - -k creates an HTTPS agent that skips certificate checks, for testing only.
Handling errors
axios throws for any 4xx or 5xx response. Wrap the call in try/catch and readerror.response.status and error.response.data to see what the API said.
Prefer the browser’s built-in API? The same command converts to JavaScript fetch, which needs no library, or to Python Requests. If the request sends Basic authentication, the Basic Auth Header Generator shows the username and password inside the header.
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.
How is curl -k handled?
It becomes an https.Agent with rejectUnauthorized: false, which skips certificate checks just like -k. Only use it for testing; never in production.
How are file uploads converted?
-F name=@file becomes a FormData field that reads the file with fs.createReadStream.
Do I need to install anything?
Yes: npm install axios. The rest uses Node.js built-ins.
Last updated