npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ansrivas/openapi-snippets

v1.0.0

Published

OpenAPI to multi-language code snippet generator

Downloads

124

Readme

OpenAPI Snippets Generator

CLI tool that takes an OpenAPI spec and injects x-codeSamples per endpoint for documentation renderers like Redoc and Swagger UI.

Installation

Global install

npm install -g .
# or publish to a registry and:
# npm install -g openapi-snippets

Then run directly:

openapi-snippets -i spec.yaml -l 'shell:curl,node:axios,python:requests'

Local (project-level)

npm install
npm run build
node dist/cli.js -i combined_output.yaml -l 'shell:curl,node:axios,python:requests'
redocly build-docs combined_output_updated.yaml --output combined_output.html

Quick Start

# Inject x-codeSamples into a copy of the spec (combined_output_updated.yaml)
openapi-snippets -i combined_output.yaml -l 'shell:curl,node:axios,python:requests'

# Target specific operations
openapi-snippets -i combined_output.yaml -l 'shell:curl,node:axios' \
  --operation-ids 'query_users,create_user'

What It Does

  1. Parses an OpenAPI 3.x spec (YAML or JSON)
  2. Generates code snippets for every endpoint using the selected languages/clients
  3. Writes a new <filename>_updated.<ext> file with x-codeSamples injected per operation
  4. Renderers like Redoc display a language selector with runnable examples for each endpoint

CLI Reference

Required:
  -i, --input <path>          OpenAPI file path
  -l, --languages <list>      Comma-separated language:client pairs

Options:
  --operation-ids <ids>       Comma-separated operation IDs to include
  --include-tags <tags>       Comma-separated tags to include
  --exclude-tags <tags>       Comma-separated tags to exclude
  --path-regex <regex>        Regex to filter paths
  --methods <methods>         Comma-separated HTTP methods (GET,POST,PUT,DELETE)
  --auth-file <path>          JSON file with auth config
  --server-index <index>      Server index from spec (default: 0)
  --server-vars <vars>        Server variable overrides (key=value,key2=value2)
  --include-optional          Include optional parameters in snippets

Supported Languages

| Language | Clients | | -------- | --------------------------------------- | | shell | curl | | node | axios, native, unirest, request | | python | requests, python3 | | java | okhttp, unirest, httpcomponents | | go | native | | csharp | httpclient, restsharp | | ruby | native, net-http | | php | curl, guzzle | | swift | nsurlsession, urlsession | | kotlin | okhttp |

Usage Examples

Inject code samples into spec

# All operations, 5 languages
openapi-snippets -i combined_output.yaml \
  -l 'shell:curl,node:axios,python:requests,go,java:okhttp'
# Outputs: combined_output_updated.yaml

Filter by tags or methods

openapi-snippets -i combined_output.yaml \
  -l 'shell:curl,node:axios' \
  --include-tags 'querytimescale_fixed_search' \
  --methods GET,POST

Authentication

Create auth.json:

{
  "bearerToken": "your-token-here"
}
openapi-snippets -i openapi.yaml -l 'shell:curl' --auth-file auth.json

Output

Writes <input>_updated.<ext> in the same directory as the input file. The original file is never modified.

x-codeSamples format

The injected x-codeSamples use the Redoc extension format:

paths:
  /api/v1/users:
    get:
      x-codeSamples:
        - lang: Shell (curl)
          label: shell-curl
          source: |
            curl --request GET \
                 --url https://example.com/api/v1/users \
                 --header 'accept: application/json'
        - lang: Node.js (axios)
          label: node-axios
          source: |
            import axios from 'axios';
            ...

Exit Codes

| Code | Meaning | | ---- | ---------------------------------------- | | 0 | All snippets generated successfully | | 1 | Failure (parse error, invalid spec, etc) |

Programmatic Usage

import { generate } from './dist/index.js';

await generate({
  input: './openapi.yaml',
  languages: [
    { language: 'shell', client: 'curl' },
    { language: 'node', client: 'axios' },
  ],
  filters: { includeTags: ['public'] },
  auth: {},
  server: { index: 0, variables: {} },
  includeOptional: false,
});
// Writes ./openapi_updated.yaml with x-codeSamples

Technical Details

  • Snippet generation: @readme/oas-to-snippet (primary) with @readme/oas-to-har + @readme/httpsnippet fallback
  • Spec parsing: oas-normalize + oas
  • YAML handling: js-yaml
  • Language target format: oasToSnippet expects [language, client] arrays, not "lang:client" strings
  • Spec output format: _updated files preserve the original format (YAML stays YAML, JSON stays JSON)

License

MIT