Generate executable code examples from OpenAPI specifications for multiple languages.
- Multi-language Support: Generate code for cURL, Python, Java, JavaScript (Fetch API), and Axios
- Smart Example Generation: Automatically handles path/query parameters and request bodies
- Type Safe: Built with TypeScript and official OpenAPI types
- Framework Agnostic: Works with any OpenAPI 3.0+ specification
- Multi-runtime Support: Run in brower (React, Vue, etc.) and Node.js.
npm install openapi-example-code-generator
# or
yarn add openapi-example-code-generator
import { OpenAPICodeGenerator } from "openapi-example-code-generator";
import type { OpenAPIObject } from "openapi-types";
const openApiSpec: OpenAPIObject = {
// Your OpenAPI specification
};
const generator = new OpenAPICodeGenerator(
openApiSpec,
"https://api.example.com"
);
// Get examples for specific language
const curlExamples = generator.getCurlExamples();
const pythonExamples = generator.getPythonExamples();
const axiosExamples = generator.getAxiosExamples();
// Get all examples
const allExamples = generator.getAllExamples();
// Override server URL from specification
const generator = new OpenAPICodeGenerator(
openApiSpec,
"https://custom-api.example.com"
);
// Generate only Python examples
const pythonCode = generator.getPythonExamples();
// Generate Axios examples with TypeScript types
const axiosCode = generator.getAxiosExamples();
Language | Method | Example Output |
---|---|---|
cURL | getCurlExamples() |
curl -X GET https://api.example... |
Python | getPythonExamples() |
import requests... |
Java | getJavaExamples() |
HttpURLConnection conn = ... |
JavaScript | getJavaScriptExamples() |
fetch(...) |
Axios | getAxiosExamples() |
axios.get(...) |
Create a new generator instance.
Parameters:
spec
: OpenAPI 3.0+ specification objectserverUrl
: Optional base URL override
getCurlExamples(): string[]
getPythonExamples(): string[]
getJavaExamples(): string[]
getJavaScriptExamples(): string[]
getAxiosExamples(): string[]
getAllExamples(): string[]
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/your-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin feature/your-feature
) - Create a new Pull Request
MIT © zxypro1