How to use the cURL to Apex Callout Converter
- Copy a cURL command from the API’s documentation, Postman (Code → cURL) or your browser’s developer tools (Copy as cURL).
- Paste it into the input. The Apex appears as you type.
- Optionally enter a Named Credential name, wrap the code in a class, and add an
HttpCalloutMockwith a test. - Copy the code or download it as a
.clsfile.
Why convert cURL to Apex?
Almost every REST API documents its endpoints as cURL commands. Turning one into Apex means rewriting the URL, method, every header and the body as HttpRequest calls, escaping quotes for Apex strings along the way. It is tedious and easy to get subtly wrong, especially with JSON bodies full of quotes.
Example
curl -X POST https://api.example.com/v1/orders \
-H 'Content-Type: application/json' \
-d '{"customerId":"C-1042"}'HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.example.com/v1/orders');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setBody('{"customerId":"C-1042"}');
HttpResponse res = new Http().send(req);Use a Named Credential, not a hard-coded URL
A Named Credential stores the endpoint and the authentication in Setup, so no secret lives in your code and admins can point the same code at a sandbox or production API. Your endpoint becomes callout:Orders_API/v1/orders, and Salesforce adds the Authorization header for you. Enter the Named Credential’s API name in the tool to generate code in this style.
Without a Named Credential, Salesforce blocks the callout until the domain is added under Setup → Remote Site Settings.
Testing callouts
Apex tests cannot make real HTTP requests. Instead, you register a class that implements HttpCalloutMock, and Salesforce returns its response whenever your code calls Http.send. Turn on “Add mock and test” to generate both the mock and a test method that uses it.
Limits worth knowing
- A single transaction can make up to 100 callouts.
- The default timeout is 10 seconds; the maximum, per callout and in total for a transaction, is 120 seconds.
- You cannot make a callout after DML in the same transaction unless the DML happens in a separate transaction, for example in a Queueable.
- Callouts from triggers must run asynchronously, for example with
@future(callout=true)or a Queueable that implementsDatabase.AllowsCallouts.
Features and limitations
What it does
- Understands
-X,-H,-d,--data-raw,--data-urlencode,--json,-u,-G,-I,--max-timeand more. - Reads shell quoting like bash does, including line continuations and escaped quotes.
- Named Credential mode rewrites the endpoint to
callout:Name/...and removes the hard-coded Authorization header. - Optional wrapper class with status-code checking, plus an
HttpCalloutMockand a ready-to-run test. - Warns about secrets in the command, Remote Site Settings, headers Salesforce manages and the 120-second timeout limit.
What it does not do
- Multipart form uploads (
-F) are not converted; Apex needs the multipart body built by hand. - Options that only affect curl itself, such as
-k,-xor-o, have no Apex equivalent and are reported. - It does not run the callout or check the endpoint, and the mock returns a simple placeholder body.
- Bodies read from files (
-d @file.json) become a placeholder you must replace.