Import and Export

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:

GroupFormats
Raw, the request as HTTP/1.1 text
ShellcURL, HTTPie, Wget
PowerShellInvoke-RestMethod
JavaScriptFetch API, Axios, XMLHttpRequest
PythonRequests, http.client
Gonet/http
JavaHttpClient, OkHttp
C#HttpClient, RestSharp
PHPcURL, Guzzle
RubyNet::HTTP
SwiftURLSession
KotlinOkHttp
Rustreqwest
Clibcurl

And these for WebSocket requests:

GroupFormats
Raw, the opening handshake as HTTP/1.1 text
Shellwebsocat
JavaScriptWebSocket (browser), ws
Pythonwebsockets
Gogorilla/websocket
JavaOkHttp
C#ClientWebSocket
SwiftURLSessionWebSocketTask
KotlinOkHttp

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 an Authorization header.
  • 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-Agent header 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 -L when the request follows redirects and --insecure when 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