Code samples
Every endpoint gets request examples in 9 languages automatically; use x-codeSamples to replace them or add your own SDK examples.
Auto-generated examples
From an endpoint's method, server URL, path parameters, query parameters, headers, authentication scheme, and request body example, ButterIX generates request code in these languages for every endpoint:
| id | Label | Notes |
|---|---|---|
curl | cURL | Command line |
python | Python | requests |
javascript | TypeScript | fetch |
go | Go | net/http |
ruby | Ruby | net/http |
java | Java | java.net.http.HttpClient |
php | PHP | Guzzle |
csharp | C# | HttpClient |
swift | Swift | URLSession |
When a reader switches language on an example card, every API example and code group on the site switches to the same language, and the choice is remembered on the next visit.
curl -X POST https://api.butterix.com/v1/orgs/acme/sites \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"name": "Acme Docs",
"slug": "acme-payments",
"template": "starter"
}'import requests
url = "https://api.butterix.com/v1/orgs/acme/sites"
payload = {
"name": "Acme Docs",
"slug": "acme-payments",
"template": "starter"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())const response = await fetch("https://api.butterix.com/v1/orgs/acme/sites", {
method: "POST",
headers: {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
},
body: JSON.stringify({
"name": "Acme Docs",
"slug": "acme-payments",
"template": "starter"
})
});
const data = await response.json();
console.log(data);package main
import (
"fmt"
"io"
"net/http"
"strings"
)
func main() {
url := "https://api.butterix.com/v1/orgs/acme/sites"
payload := strings.NewReader(`{
"name": "Acme Docs",
"slug": "acme-payments",
"template": "starter"
}`)
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}require "uri"
require "net/http"
require "json"
url = URI("https://api.butterix.com/v1/orgs/acme/sites")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = "Bearer <token>"
request["Content-Type"] = "application/json"
request.body = {
"name": "Acme Docs",
"slug": "acme-payments",
"template": "starter"
}.to_json
response = http.request(request)
puts response.read_bodyHttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.butterix.com/v1/orgs/acme/sites"))
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\"name\":\"Acme Docs\",\"slug\":\"acme-payments\",\"template\":\"starter\"}"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());<?php
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'https://api.butterix.com/v1/orgs/acme/sites', [
'body' => '{"name":"Acme Docs","slug":"acme-payments","template":"starter"}',
'headers' => [
'Authorization' => 'Bearer <token>',
'Content-Type' => 'application/json',
],
]);
echo $response->getBody();using var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.butterix.com/v1/orgs/acme/sites");
request.Headers.Add("Authorization", "Bearer <token>");
request.Content = new StringContent("{\"name\":\"Acme Docs\",\"slug\":\"acme-payments\",\"template\":\"starter\"}", Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
Console.WriteLine(await response.Content.ReadAsStringAsync());import Foundation
var request = URLRequest(url: URL(string: "https://api.butterix.com/v1/orgs/acme/sites")!)
request.httpMethod = "POST"
request.setValue("Bearer <token>", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = """
{
"name": "Acme Docs",
"slug": "acme-payments",
"template": "starter"
}
""".data(using: .utf8)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))Choose which languages to show
All nine are shown by default. List the languages to show, in order, with code-languages in docs.yml; anything not listed is hidden:
code-languages: [curl, python, typescript, go]- Use an
idfrom the table above or an alias (such astypescript,golang,c#). Addingpayloadgives an extra entry with just the request data. - The request examples on endpoint pages and the Try it panel use the same list.
- In the console: Docs settings → API reference → Code example languages in the editor.
Provide your own examples with x-codeSamples
If you have an official SDK, you usually want to show SDK usage instead of raw HTTP requests. Add x-codeSamples to the operation (a Redocly convention; x-code-samples works too):
paths:
/payments:
post:
summary: Create a payment
x-fern-sdk-method-name: create-payment
x-codeSamples:
- lang: typescript
label: Node SDK
source: |
const payment = await acme.payments.create({ amount: 1999, currency: "cny" });
- lang: python
label: Python SDK
source: |
payment = acme.Payment.create(amount=1999, currency="cny")
- lang: console
label: Acme CLI
source: |
$ acme payments create --amount 1999 --currency cnyMatching rules:
- When
langmaps to a built-in language, it replaces that language's generated example and the label becomeslabel. Aliases:shell/bash/sh→ cURL,typescript/ts/js/node→ TypeScript,py→ Python,golang→ Go,rb→ Ruby,c#/cs→ C#. - A
langthat doesn't map to a built-in language (likeconsoleabove) is appended as an extra tab at the end, andlangis also used as the highlighting language. - If you write several examples for the same language, only the last one takes effect.
This site's Create deployment endpoint demonstrates both cases: a shell example replaces the generated cURL, and a console example is appended as a "biti CLI" tab at the end.
tar --exclude=node_modules --exclude=.git --exclude=dist -czf site.tgz .
curl -X POST https://api.butterix.com/v1/sites/acme-payments/deployments \
-H "Authorization: Bearer $BITI_TOKEN" \
-H "Content-Type: application/gzip" \
-H "x-biti-preview: 1" \
--data-binary @site.tgzimport requests
url = "https://api.butterix.com/v1/sites/acme-payments/deployments"
payload = {
"preview": True,
"branch": "docs/new-quickstart"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())const response = await fetch("https://api.butterix.com/v1/sites/acme-payments/deployments", {
method: "POST",
headers: {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
},
body: JSON.stringify({
"preview": true,
"branch": "docs/new-quickstart"
})
});
const data = await response.json();
console.log(data);package main
import (
"fmt"
"io"
"net/http"
"strings"
)
func main() {
url := "https://api.butterix.com/v1/sites/acme-payments/deployments"
payload := strings.NewReader(`{
"preview": true,
"branch": "docs/new-quickstart"
}`)
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}require "uri"
require "net/http"
require "json"
url = URI("https://api.butterix.com/v1/sites/acme-payments/deployments")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = "Bearer <token>"
request["Content-Type"] = "application/json"
request.body = {
"preview": true,
"branch": "docs/new-quickstart"
}.to_json
response = http.request(request)
puts response.read_bodyHttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.butterix.com/v1/sites/acme-payments/deployments"))
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\"preview\":true,\"branch\":\"docs/new-quickstart\"}"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());<?php
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'https://api.butterix.com/v1/sites/acme-payments/deployments', [
'body' => '{"preview":true,"branch":"docs/new-quickstart"}',
'headers' => [
'Authorization' => 'Bearer <token>',
'Content-Type' => 'application/json',
],
]);
echo $response->getBody();using var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.butterix.com/v1/sites/acme-payments/deployments");
request.Headers.Add("Authorization", "Bearer <token>");
request.Content = new StringContent("{\"preview\":true,\"branch\":\"docs/new-quickstart\"}", Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
Console.WriteLine(await response.Content.ReadAsStringAsync());import Foundation
var request = URLRequest(url: URL(string: "https://api.butterix.com/v1/sites/acme-payments/deployments")!)
request.httpMethod = "POST"
request.setValue("Bearer <token>", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = """
{
"preview": true,
"branch": "docs/new-quickstart"
}
""".data(using: .utf8)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))$ biti deploy --site acme-payments --message "Release v1.2"Filter languages in guides
The languages prop on <EndpointRequestSnippet> sets which languages appear and in what order; adding payload adds a tab that shows only the request body JSON:
<EndpointRequestSnippet
endpoint="PUT /sites/{site}/ai"
example="deepseek"
languages={["curl", "python", "payload"]}
/>curl -X PUT https://api.butterix.com/v1/sites/acme-payments/ai \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"provider": "deepseek",
"model": "deepseek-flash",
"apiKey": "sk-xxxxxxxxxxxxxxxx"
}'import requests
url = "https://api.butterix.com/v1/sites/acme-payments/ai"
payload = {
"provider": "deepseek",
"model": "deepseek-flash",
"apiKey": "sk-xxxxxxxxxxxxxxxx"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.put(url, json=payload, headers=headers)
print(response.json()){
"provider": "deepseek",
"model": "deepseek-flash",
"apiKey": "sk-xxxxxxxxxxxxxxxx"
}example selects an entry from the spec's examples by name (or summary); the example above selects the request example named deepseek.
Values in examples
| Location | Value |
|---|---|
| Base URL | The first entry of operation-level servers → path-level servers → top-level servers (with server variables replaced by their defaults) |
| Path parameters, required query parameters, and headers | The parameter's example / examples → schema example (optional query parameters and headers are omitted) |
| Authentication | Bearer schemes produce Authorization: Bearer <token>; API key schemes go into a header or query parameter based on in |
| Request body | The first entry in examples (or the selected example) → example → generated from the schema (required fields only) |
Give schema fields realistic example values and the generated examples become much more readable. This site's spec does exactly that with name: { type: string, example: Acme 文档 }.