# @readme/oas-to-snippet

> Transform an OpenAPI operation into a code snippet

Latest version **39.0.0** (published 2026-10-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install @readme/oas-to-snippet
pnpm add @readme/oas-to-snippet
yarn add @readme/oas-to-snippet
bun add @readme/oas-to-snippet
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 39.0.0 |
| Published | 2026-10-22 |
| First published | 2020-07-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20 |
| Dependencies | 2 |
| Unpacked size | 88.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 401 |
| Author | Jon Ursenbach |
| Maintainers | gkoberger, domharrington, mjcuva, jonursenbach, rafegoldberg, darrenyong |

## Links

- npm: https://www.npmjs.com/package/@readme/oas-to-snippet
- Repository: https://github.com/readmeio/oas
- Homepage: https://github.com/readmeio/oas#readme
- Issues: https://github.com/readmeio/oas/issues
- npm.io page: https://npm.io/package/@readme/oas-to-snippet

## Dependencies (2)

- [@readme/oas-to-har](https://npm.io/package/@readme/oas-to-har.md) ^39.0.0
- [@readme/httpsnippet](https://npm.io/package/@readme/httpsnippet.md) ^11.4.0

## Recent versions

- 39.0.0 (latest) — 2026-10-22
- 38.2.0 — 2026-08-04
- 38.0.0 — 2026-07-07
- 37.0.0 — 2026-06-25
- 36.0.0 — 2026-06-17
- 35.0.0 — 2026-06-16
- 34.1.0 — 2026-06-15
- 34.0.0 — 2026-05-28
- 33.0.0 — 2026-05-01
- 32.1.14 — 2026-04-15
- 32.1.13 — 2026-04-15
- 32.1.12 — 2026-04-13
- 32.0.14 — 2026-04-10
- 32.0.13 — 2026-04-07
- 32.0.12 — 2026-04-07
- … 230 more at https://npm.io/package/@readme/oas-to-snippet/versions

## README

# @readme/oas-to-snippet

Transform an OpenAPI operation into a code snippet. This library was built with [ReadMe's reference guide](https://readme.com/documentation) offering in mind but it will support all OpenAPI use-cases.

[![npm](https://img.shields.io/npm/v/@readme/oas-to-snippet)](https://npm.im/@readme/oas-to-snippet) [![Build](https://github.com/readmeio/oas/workflows/CI/badge.svg)](https://github.com/readmeio/oas/tree/main/packages/oas-to-snippet)

<a href="https://readme.com">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/readmeio/.github/main/oss-header-dark.png">
  <source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/readmeio/.github/main/oss-header.png">
  <img alt="ReadMe Open Source" src="https://raw.githubusercontent.com/readmeio/.github/main/oss-header.png">
</picture>
</a>

## Installation

```sh
npm install --save @readme/oas-to-snippet
```

## Usage

```js
import Oas from 'oas';
import oasToSnippet from '@readme/oas-to-snippet';
import petstore from './petstore.json';

const apiDefinition = new Oas(petstore);
const operation = apiDefinition.operation('/pets', 'get');

// This is a keyed object containing formData for your operation. Available keys
// are: path, query, cookie, header, formData, and body.
const formData = {
  query: { sort: 'desc' },
};

// This is a keyed object containing authentication credentials for the
// operation. The keys for this object should match up with the `securityScheme`
// on the operation you're accessing, and its value should either be a String,
// or an Object containing `user` and/or `pass` (for Basic auth schemes).
const auth = {
  oauth2: 'bearerToken',
};

// This is the language to generate a snippet to. See below for supported
// languages.
//
// For supplying an alternative language target (like `axios` for `node`), you
// can do so by changing this variable to an array: `['node', 'axios']`. For the
// full list of alternative language targets that we support, see below.
const language = 'node';

// This will return an object containing `code` and `highlightMode`. `code` is
// the generated code snippet, while `highlightMode` is the language mode you
// can use to render it for syntax highlighting (with `codemirror` for example).
const { code, highlightMode } = oasToSnippet(apiDefinition, operation, formData, auth, language);
```

### Plugins

This library also supports the plugin system that we've built into [HTTPSnippet](https://npm.im/@readme/httpsnippet). We have a plugin for generating snippets for [ReadMe's API SDK generator](https://api.readme.dev) and this is how you would integrate and generate snippets for it:

```js
import oasToSnippet from '@readme/oas-to-snippet';
import httpsnippetClientAPIPlugin from 'httpsnippet-client-api';

const snippet = oasToSnippet(
  petstore,
  petstore.operation('/user/login', 'get'),
  formData,
  auth,
  // `[node, api]` is not a standard language in `oas-to-snippet` but is
  // dynamically made available via this loaded plugin.
  ['node', 'api'],
  {
    openapi: {
      registryIdentifier: '@petstore/v2.0#17273l2glm9fq4l5',
    },
    plugins: [httpsnippetClientAPIPlugin],
  },
);
```

## Supported Languages

Since this library uses [HTTPSnippet](https://npm.im/@readme/httpsnippet) we support most of its languages and their associated targets which are the following:

<!--
To regenerate the table below, run the following:

npm run build && node bin/generate-target-markdown-table.js
 -->

<!-- prettier-ignore-start -->
<!-- table-start -->
| Language | Available language mode(s) | Libraries (if applicable)
| :---- | :---- | :---- |
| C | `c` | [Libcurl](http://curl.haxx.se/libcurl)
| Clojure | `clojure` | [clj-http](https://github.com/dakrone/clj-http)
| C++ | `cplusplus` | [Libcurl](http://curl.haxx.se/libcurl)
| C# | `csharp` | [HttpClient](https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient), [RestSharp](http://restsharp.org/)
| HTTP | `http` | [HTTP/1.1](https://tools.ietf.org/html/rfc7230)
| Go | `go` | [NewRequest](http://golang.org/pkg/net/http/#NewRequest)
| Java | `java` | [AsyncHttp](https://github.com/AsyncHttpClient/async-http-client), [java.net.http](https://openjdk.java.net/groups/net/httpclient/intro.html), [OkHttp](http://square.github.io/okhttp/), [Unirest](http://unirest.io/java.html)
| JavaScript | `javascript` | [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest), [Axios](https://github.com/axios/axios), [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch), [jQuery](http://api.jquery.com/jquery.ajax/)
| JSON | `json` | [Native JSON](https://www.json.org/json-en.html)
| Kotlin | `kotlin` | [OkHttp](http://square.github.io/okhttp/)
| Node.js | `node` | [HTTP](http://nodejs.org/api/http.html#http_http_request_options_callback), [Axios](https://github.com/axios/axios), [fetch](https://nodejs.org/docs/latest/api/globals.html#fetch)
| Objective-C | `objectivec` | [NSURLSession](https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html)
| OCaml | `ocaml` | [CoHTTP](https://github.com/mirage/ocaml-cohttp)
| PHP | `php` | [cURL](http://php.net/manual/en/book.curl.php), [Guzzle](http://docs.guzzlephp.org/en/stable/), [HTTP v1](http://php.net/manual/en/book.http.php), [HTTP v2](http://devel-m6w6.rhcloud.com/mdref/http)
| Powershell | `powershell` | [Invoke-WebRequest](https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-WebRequest), [Invoke-RestMethod](https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-RestMethod)
| Python | `python` | [Requests](http://docs.python-requests.org/en/latest/api/#requests.request)
| R | `r` | [httr](https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html)
| Ruby | `ruby` | [net::http](http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html)
| Shell | `shell` | [cURL](http://curl.haxx.se/), [HTTPie](http://httpie.org/), [Wget](https://www.gnu.org/software/wget/)
| Swift | `swift` | [URLSession](https://developer.apple.com/documentation/foundation/urlsession)
<!-- table-end -->
<!-- prettier-ignore-end -->

---
_Source: https://npm.io/package/@readme/oas-to-snippet · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
