Skip to content

cURL to Apex Callout Converter

Paste a cURL command from any API’s docs and get a working Apex HTTP callout, mock and test.

Runs in your browser. Nothing is uploaded.

cURL command

Recommended. The endpoint becomes callout:Name/… and the secret stays out of your code.

Try an example

Apex callout

40 lines
public with sharing class ExampleCallout {
    public static HttpResponse send() {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://api.example.com/v1/orders');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Authorization', 'Bearer YOUR_TOKEN');
        req.setBody('{"customerId": "C-1042", "items": [{"sku": "LAB-01", "qty": 2}]}');
        req.setTimeout(20000);

        HttpResponse res = new Http().send(req);
        if (res.getStatusCode() < 200 || res.getStatusCode() > 299) {
            throw new CalloutException('Callout failed: ' + res.getStatusCode() + ' ' + res.getStatus());
        }
        return res;
    }
}

@IsTest
public class ExampleCalloutMock implements HttpCalloutMock {
    public HttpResponse respond(HttpRequest req) {
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"success":true}');
        res.setStatusCode(200);
        return res;
    }
}

@IsTest
private class ExampleCalloutTest {
    @IsTest
    static void returnsSuccess() {
        Test.setMock(HttpCalloutMock.class, new ExampleCalloutMock());
        Test.startTest();
        HttpResponse res = ExampleCallout.send();
        Test.stopTest();
        Assert.areEqual(200, res.getStatusCode());
    }
}

2 things to check

  • The generated code contains a secret in the Authorization header. Do not commit it; use a Named Credential instead.
  • Before this runs, allow https://api.example.com in Setup → Remote Site Settings, or use a Named Credential (recommended).

How to use the cURL to Apex Callout Converter

  1. Copy a cURL command from the API’s documentation, Postman (Code → cURL) or your browser’s developer tools (Copy as cURL).
  2. Paste it into the input. The Apex appears as you type.
  3. Optionally enter a Named Credential name, wrap the code in a class, and add an HttpCalloutMock with a test.
  4. Copy the code or download it as a .cls file.

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
curl -X POST https://api.example.com/v1/orders \
  -H 'Content-Type: application/json' \
  -d '{"customerId":"C-1042"}'
Apex
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 implements Database.AllowsCallouts.

Features and limitations

What it does

  • Understands -X, -H, -d, --data-raw, --data-urlencode, --json, -u, -G, -I, --max-time and 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 HttpCalloutMock and 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, -x or -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.

Questions about the cURL to Apex Callout Converter

No. The command is parsed in your browser and nothing is uploaded. Still, treat generated code that contains a token as a secret, and prefer a Named Credential.

Most API documentation shows one. In Postman, open the Code panel and choose cURL. In Chrome or Edge developer tools, right-click a request in the Network tab and choose Copy → Copy as cURL (bash).

Salesforce blocks callouts to any domain that is not allowed by a Remote Site Setting or covered by a Named Credential. The warning reminds you to add one before testing.

Commands copied for bash work best. Line continuations with ^ are understood, but Windows-style double quoting with doubled quotes may need adjusting.

When you wrap the code in a class, it throws a CalloutException for any status outside 200–299, so callers can catch failures in one place.