# Code snippets

> Generate code for any API client request in 22 languages plus Markdown, such as cURL, Python, Go, Swift and k6, with or without variables filled in.

Source: https://docs.subcloak.com/cloak-proxy/api-client/code-snippets/

Code snippets turn a request into code you can paste into an app, a script or a bug report. Pick one of 22 languages, or Markdown, and the client writes the same method, URL, headers and body in that language's usual HTTP library or test tool.

## Get a snippet

1. Open an HTTP or GraphQL request.
2. Click **Code** in the request's header.
3. Pick a language from the menu. The client remembers your choice for next time.
4. Click **Copy**, then **Done**.

*[Screenshot: The Code snippet sheet with cURL selected, Fill in variables checked, a two-line curl command for a pets URL, the note that auth headers aren't in the snippet, and Copy and Done buttons. The Code snippet sheet.]*

For a quick cURL command, right-click a request in the sidebar and choose **Copy as cURL**. It copies the request with variables filled in.

## Languages

| Menu name | Produces |
|---|---|
| **cURL** | A `curl` command for the shell. |
| **Swift** | `URLRequest` and `URLSession` with `async`/`await`. |
| **JavaScript** | `fetch`. |
| **Python** | The `requests` library. JSON bodies become a Python dictionary. |
| **Go** | `net/http`. |
| **HTTPie** | An `http` command. |
| **Shell – wget** | A `wget` command. |
| **HTTP** | The raw HTTP request. |
| **NodeJS – Axios** | Axios. |
| **Python – http.client** | Python's standard-library `http.client`. |
| **Java – OkHttp** | OkHttp. |
| **Kotlin – OkHttp** | OkHttp from Kotlin. |
| **C# – HttpClient** | .NET `HttpClient`. |
| **PHP – cURL** | PHP's cURL functions. |
| **Ruby – Net::HTTP** | Ruby's `Net::HTTP`. |
| **Rust – reqwest** | The `reqwest` crate. |
| **Dart – http** | The `http` package. |
| **PowerShell** | `Invoke-RestMethod`. |
| **C – libcurl** | libcurl from C. |
| **k6 – load test** (added in Cloak Proxy 0.2.0) | A k6 script that sends the request with 10 virtual users for 30 seconds and checks for a 2xx status. |
| **Python – httpx** (added in Cloak Proxy 0.2.0) | The `httpx` library. JSON bodies become a Python dictionary. |
| **Playwright – API test** (added in Cloak Proxy 0.2.0) | A `@playwright/test` test that sends the request and expects a successful status. |
| **Markdown** | A heading with the method and URL, a table of headers and the body in a code block, for docs and issues. |

## What's in a snippet

The snippet is the request as it would be sent, minus anything that only happens when you click **Send**.

| Included | Not included |
|---|---|
| Method and URL, with query params | Auth from the **Authorization** tab, or inherited from a folder or collection |
| Enabled headers | Pre-request and post-response scripts, and their changes |
| The body, when it's text | Cookies from the client's cookie jar |
| The `Content-Type` the body type adds | Request settings such as timeouts and TLS verification |
| Path variable values | |

Except in **cURL** and **Markdown**, snippets leave out the `Host`, `Content-Length`, `Connection`, `Proxy-Connection` and `Accept-Encoding` headers, which HTTP libraries set themselves.

To include auth in a snippet, add the header yourself on the **Headers** tab, for example `Authorization: Bearer {{token}}`. The sheet reminds you: "Auth headers are added when the request is sent, so they aren't in the snippet unless set as headers."

### Fill in variables

**Fill in variables** is on by default. It replaces `{{variables}}` with their current values from the active environment, collection and globals, along with Vault secrets and fresh dynamic values. Turn it off to keep `{{baseUrl}}` and the rest as written, which is better for documentation and for sharing. Path variables are filled in either way.

> **Snippets can contain real secrets:**
> With **Fill in variables** on, a snippet contains the real values of secret variables and `{{vault:…}}` references. **Copy as cURL** always fills them in. Turn the option off, or check the code, before you paste it into a chat, an issue or a document.

## Other places to get code

- **Exported HTML docs** include a snippet for every request in ten of these languages, with variables kept as written. See [Examples and docs](https://docs.subcloak.com/cloak-proxy/api-client/examples-and-docs/#export-html-documentation).
- **Captured traffic** in the proxy can be copied as code too. See [Copy as code](https://docs.subcloak.com/cloak-proxy/inspect/copy-as-code/).
- **Whole client libraries**: **Generate SDK** on a collection writes a TypeScript, Python or Swift SDK. See [Generate docs, SDKs and MCP servers](https://docs.subcloak.com/cloak-proxy/api-client/run/generate-docs-sdks-and-mcp-servers/).

## Related

  - [Copy as code](https://docs.subcloak.com/cloak-proxy/inspect/copy-as-code/): Code from captured requests.
  - [Variables and environments](https://docs.subcloak.com/cloak-proxy/api-client/variables-and-environments/): What Fill in variables uses.
  - [Authorization](https://docs.subcloak.com/cloak-proxy/api-client/authorization/): Why auth isn't in snippets.
  - [Generate docs, SDKs and MCP servers](https://docs.subcloak.com/cloak-proxy/api-client/run/generate-docs-sdks-and-mcp-servers/): Generate whole client libraries.
