Fragments

API Snippet

Composed, tabbed code reference for an API call, with pure curl, HTTPie and TypeScript SDK generators.

Formance
Docs

ApiSnippet is a composed, tabbed code reference for an API call. It knows no API: the parent passes any number of <ApiSnippetTab value label language code /> children, plus free header and footer content (a title, an Endpoint, a version, setup steps). The component keeps the copy button for the active tab, the tab switch and the code-height clipping. A single tab shows no tab switch.

The tab code comes from pure generators, exported from _api-snippet/generators. Each generator takes a resolved operation description and returns a string:

type TResolvedOperation = {  method: string;  baseUrl: string;  path: string; // path parameters already applied  query: [string, unknown][]; // ordered; objects and arrays are JSON  headers: Record<string, string>;  body?: unknown;  bodyFile?: string;  rawArgs?: string;  pathParams?: Record<string, string>; // SDK arguments  sdk?: { module: string; method: string }; // SDK call, e.g. ledgerV2.listAccounts};
generateCurl(request); // curl -G "<url>" --data-urlencode 'name=value' …generateHttpie(request); // http "<url>" 'name==value' …generateSdk(request); // await sdk.ledger.v2.listAccounts({ … }), or '' without `sdk`
  • A GET sends each query parameter with --data-urlencode (curl) or name==value (HTTPie).
  • A JSON value (an object or array query value, or the body) is compact when name=<json> fits in 80 characters. Otherwise it is indented over several lines inside the single quotes.
  • A single quote in a value is escaped for a POSIX shell ('\'').
  • A body gives -H "Content-Type: application/json" and -d '<json>' (curl). In HTTPie, each top-level field of an object body is a request item: 'name=value' for a string, 'name:=<json>' for anything else; a body that is not an object falls back to --raw '<json>'. bodyFile gives -d @file or < file. rawArgs is appended as is.
  • The SDK snippet uses the public @formance/formance-sdk shape: sdk.<module>.<version>.<method>.

Operations index#

resolveOperation(operation, { params, query, body, bodyFile, rawArgs, baseUrl, headers }) turns an entry of an operations index into a TResolvedOperation. The stack index ships as lib/stack-operations.json (registry item stack-operations, re-generated with pnpm extract:operations). A membership/cloud index ships as lib/membership-operations.json (membership-operations, pnpm extract:membership-operations); its entries declare no SDK.

const { operations } = stackOperationsData as TStackOperationsIndex;const request = resolveOperation(operations.v2CreateLedger!, {  params: { ledger: 'main' },  body,  headers: { Authorization: 'Bearer $AUTH_TOKEN' },});
<ApiSnippet  clipCodeHeight  footer={<Endpoint method={request.method} path={request.path} />}>  <ApiSnippetTab    value="curl"    label="curl"    language="bash"    code={generateCurl(request)}  />  <ApiSnippetTab    value="httpie"    label="HTTPie"    language="bash"    code={generateHttpie(request)}  />  <ApiSnippetTab    value="sdk"    label="TypeScript"    language="typescript"    code={generateSdk(request)}  /></ApiSnippet>;

The active tab is uncontrolled by default (seeded by defaultValue, else the first tab). Pass value + onValueChange to control it from the host.

GET request#

A GET with ordered query parameters: curl uses -G and one --data-urlencode per parameter, and a short JSON filter stays compact.

Without fctl tab#

Pass only the tabs you want. Here curl, HTTPie and the TypeScript SDK.

Custom tabs#

The children set the tabs and their order. Here fctl and curl, with defaultValue selecting fctl.

Stack API#

A request resolved from the stack operations index, with a header title, an Endpoint footer and an fctl tab.

Membership API#

A request resolved from the membership operations index. Its operations declare no SDK, so there is no TypeScript tab.

fctl only#

A single tab: the tab switch is not shown.

SDK as default tab#

Set defaultValue='sdk' when the SDK call is the canonical example.

Clipped code height#

Set clipCodeHeight when the snippet lives inside a constrained surface (dialog, sidebar), so a tall body scrolls inside the box.

Hand-built request#

Build a TResolvedOperation by hand for an endpoint that isn't in an operations index. The long $and filter is indented.

Body from a file#

bodyFile references the request body from a file: curl renders -d @file and HTTPie renders < file.

Raw args#

rawArgs appends extra flags to the generated command, e.g. --fail-with-body for curl.

Controlled tabs#

Pass value + onValueChange to control the active tab from the host. Here two snippets share one tab state.