@scalar/snippetz
v0.10.17
Published
Keywords
Readme
Snippetz
A modern way to generate HTTP request examples for different languages and libraries.
Scalar is an open-source API platform for teams who want beautiful developer interfaces without vendor lock-in.
- API References — Interactive API documentation from OpenAPI and AsyncAPI specs.
- Developer Docs — Write in Markdown/MDX, generate API references, sync with two-way Git.
- SDK Generator — Type-safe SDKs and CLIs in TypeScript, Python, Go, PHP, Java, and Ruby.
- API Client — Open-source, offline-first Postman alternative built on OpenAPI.
20M+ monthly npm installs · 15,500+ GitHub stars · MIT licensed · scalar.com
Installation
npm install @scalar/snippetzUsage
import { snippetz } from '@scalar/snippetz'
const snippet = snippetz().print('node', 'undici', {
url: 'https://example.com',
})
/* Output */
// import { request } from 'undici'
//
// const { statusCode, body } = await request(
// 'https://example.com',
// )API
Get all plugins
import { snippetz } from '@scalar/snippetz'
const snippet = snippetz().plugins()
/* Output */
// [
// {
// target: 'node',
// client: 'undici',
// }
// ]Check if a plugin is loaded
import { snippetz } from '@scalar/snippetz'
const snippet = snippetz().hasPlugin('node', 'undici')
/* Output */
// trueLean usage
You can also just use one specific plugin to keep your bundle size small.
import { nodeUndici } from '@scalar/snippetz/plugins/node/undici'
const result = nodeUndici.generate({
url: 'https://example.com',
})
console.log(source)
// import { request } from 'undici'
// const { statusCode, body } = await request(
// 'url': 'https://example.com',
// )Community
We are API nerds. You too? Let's chat on Discord: https://discord.gg/scalar
License
The source code in this repository is licensed under MIT.
Cookies in browser examples
XHR and jQuery examples use document.cookie and enable withCredentials whenever the HAR contains cookie values, including an explicit Cookie header without OpenAPI cookie-style parameters. This intentionally changes those legacy examples: browsers forbid setting the Cookie request header directly. Requests without cookies remain uncredentialed.
The setup writes cookies for the page's domain and cannot set cookies for an unrelated API domain or create HttpOnly cookies. For a cross-origin API, obtain cookies through that API's authentication flow instead. Cookie domain, path, Secure, SameSite, and browser third-party-cookie policies still apply.
Credentialed cross-origin responses must permit credentials and the specific calling origin through CORS (Access-Control-Allow-Credentials: true and a non-wildcard Access-Control-Allow-Origin). Enabling credentials does not itself require preflight; if the request otherwise requires preflight, that check must also succeed.
Cookie-style values remain serialized as authored, while legacy structured HAR cookies are percent-encoded. Do not decode and re-encode a combined Cookie header uniformly. Other generators retain their existing environment-specific header behavior.
For OpenAPI 3.2 in: cookie, style: cookie parameters, omit explode or set it to true. explode: false is invalid for cookies because comma-separated values violate cookie syntax. Scalar tolerates that invalid combination by expanding arrays and objects into separate cookie entries; it emits a developer-console warning once per parameter name. Values are passed through unchanged, so provide any required escaping in the API description.
Query parameters
HAR queryString entries contain raw names and values. Pass 2026-09-30T02:00:00Z, not 2026-09-30T02%3A00%3A00Z. URL-based examples encode these entries when constructing the URL; clients with query maps handle encoding themselves. A literal %2F value stays literal data and becomes %252F on the wire.
Already serialized query strings belong in url, with queryString left empty. Scalar uses that path for OpenAPI allowReserved parameters and authored serialized examples.
