Skip to content

Code snippets ​

Turn a request you have working in ApiWorkbench into code you can paste into a script or an app.

Generate a snippet ​

  1. Open the HTTP request.

  2. Select the More menu (the three dots next to Send) and choose Code snippet….

  3. Pick a language and library under Language.

  4. Select Copy, then paste the code where you need it.

  5. Select Close.

The code shows the request as it would be sent: variables, path variables and auth are applied, so what you see is what goes on the wire.

The 19 languages ​

LanguageLibrary
ShellcURL
ShellHTTPie
Shellwget
ShellPowerShell
Pythonrequests
Pythonhttp.client
JavaScriptfetch
JavaScriptaxios
JavaScriptNode.js https
Javajava.net.http
JavaOkHttp
KotlinOkHttp
C#HttpClient
Gonet/http
PHPcURL
RubyNet::HTTP
SwiftURLSession
Darthttp
Rustreqwest

The list is in this order. Each entry reads as language and library, for example Python – requests.

Resolved or with variables ​

By default the snippet contains real values. Turn on Show {{variables}} unresolved to keep the placeholders as you wrote them.

SettingThe snippet containsUse it when
Off (default)The current value of every variable, and the finished auth headers.You want code that runs as it is.
On{{name}} wherever you wrote it.The code will read the values from somewhere else, or you want to share it.

Example with the setting on:

bash
curl 'https://{{host}}/users' \
  -H 'Authorization: Bearer {{token}}'

DANGER

A resolved snippet contains your real tokens, passwords and API keys. Do not paste it into a public chat, an issue or a repository. Turn on the unresolved option first, or replace the secrets by hand.

What is included ​

  • The method, URL with query params, and headers.
  • Auth from the request, or the auth it inherits from its folder or collection.
  • The body in the shape the language wants: text and JSON bodies as written, form-urlencoded fields, multipart form-data with text and file parts, and a binary body read from its file.

File parts and binary bodies refer to the file by its path. The snippet is generated even if the file is not there, so check the path on the machine that will run the code.

If something could not be applied, such as auth that needs information that is missing, a note appears above the code. If the request cannot be built at all, for example because the URL is empty, the dialog says "The code cannot be generated yet" with the reason.

Good to know

Code snippets are for HTTP requests. WebSocket, Socket.IO, MQTT, Server-Sent Events and gRPC tabs have no code snippet.

Snippet or Copy as cURL? ​

The More menu also has Copy as cURL. It copies at once, without a dialog, and always keeps {{variables}} as written. See cURL in and out.

Next steps ​