Generating code from requests
Turn a request into code for curl, Python, JavaScript, Swift and other languages.
Overview
Once a request works in Relay, you can take it elsewhere: into a shell script, a test, your app's code or a bug report. The Preview panel at the bottom of the request editor shows the request as raw HTTP or as code in another language, and updates as you edit.
The code describes the request as Relay would send it, with its variables, inherited authentication, settings and cookies applied. Relay never runs the request's scripts to make it, so changes a pre-request script would make don't appear in the code.
Show the Preview panel
In the bar at the bottom of the request editor, click the request's method and URL. Click them again to hide the panel.
The format menu at the top of the panel lists the generators for the request's kind, grouped by language. Choose one, and the panel shows the code. Relay remembers your choice, one for HTTP requests and one for WebSocket requests, and starts with Raw for both.
To copy the code, click Copy Preview. To save it to a file, click Save Preview; the file name starts with the request's name and ends with the language's extension.
Choose a format
These generators come with Relay for HTTP requests:
| Group | Formats |
|---|---|
| Raw, the request as HTTP/1.1 text | |
| Shell | cURL, HTTPie, Wget |
| PowerShell | Invoke-RestMethod |
| JavaScript | Fetch API, Axios, XMLHttpRequest |
| Python | Requests, http.client |
| Go | net/http |
| Java | HttpClient, OkHttp |
| C# | HttpClient, RestSharp |
| PHP | cURL, Guzzle |
| Ruby | Net::HTTP |
| Swift | URLSession |
| Kotlin | OkHttp |
| Rust | reqwest |
| C | libcurl |
And these for WebSocket requests:
| Group | Formats |
|---|---|
| Raw, the opening handshake as HTTP/1.1 text | |
| Shell | websocat |
| JavaScript | WebSocket (browser), ws |
| Python | websockets |
| Go | gorilla/websocket |
| Java | OkHttp |
| C# | ClientWebSocket |
| Swift | URLSessionWebSocketTask |
| Kotlin | OkHttp |
The WebSocket generators connect, print what the server sends and close; they don't send the request's saved messages.
To add a format of your own, or change one of these, see Creating a custom code generator.
Show or hide variables and credentials
Click Preview Options (the gear) to choose what the code reveals:
- Reveal Variables: When it's on,
{{name}}tokens are replaced with their values, as when you send. Turn it off to keep the tokens as written, for code you share with people who use other values. - Reveal Credentials: When it's on, the code holds the request's credentials. Turn it off to replace each one with
••••••••, for code you paste into a ticket or a chat.
Both settings apply to every request and are on until you turn them off.
Understand how authentication appears
Relay writes the request's authentication the way the target client expresses it best:
- Bearer Token and Basic Auth: Generators use the client's own option where it has one, such as
curl -u, or anAuthorizationheader. - API Key, JWT Bearer and OAuth 2.0: The headers or query parameters they add appear like any other. For OAuth 2.0, the code uses the access token Relay already has; generating code never fetches or refreshes one.
- AWS Signature, OAuth 1.0 and Hawk: Relay signs the request when it generates the code. Servers accept a signature only for a short time, so the code includes a note to sign each request in your own code.
- Digest Auth and NTLM: These answer the server's challenge, so they can't be written ahead of time. Clients that support the scheme use it; for others, the code includes a note.
An Authorization header you add yourself replaces the request's authentication, in the code as when you send.
Check what else is included
- The cookies the workspace would send to the request's URL.
- The default
User-Agentheader set in the Requests pane of Relay > Settings, unless the request sets its own. - The request's timeout, redirect and certificate settings, for clients that have options for them. For example, the cURL generator adds
-Lwhen the request follows redirects and--insecurewhen it doesn't verify certificates. - For a file in a multipart form, the file's path, as you chose it. Relay doesn't read the file to make the code.
If a generator can't turn the request into code, the panel says why instead. Messages a generator writes to its console appear above the code, each starting with //.
Generate code from an AI agent
An AI agent connected to Relay's MCP server can list the generators with the list_generators tool and generate code for a request with generate_code. For a list of tools, see MCP Tools.
See Also
- Creating a custom code generator: Write a JavaScript generator that turns requests into code in the form you need.
- Using variables: Insert values into requests with
{{name}}tokens and see where each value comes from. - Inheriting authentication: Set authentication once on a workspace, collection or folder and let requests inherit it.
- Importing cURL commands: Turn a cURL command into a draft request.