https://github.com/zxypro1/openapi-example-code-generator
Generate cUrl, Python, Java, JavaScript (and more...) code examples from one OpenAPI spec
https://github.com/zxypro1/openapi-example-code-generator
Last synced: 5 months ago
JSON representation
Generate cUrl, Python, Java, JavaScript (and more...) code examples from one OpenAPI spec
- Host: GitHub
- URL: https://github.com/zxypro1/openapi-example-code-generator
- Owner: zxypro1
- License: mit
- Created: 2025-03-29T06:08:24.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-29T06:16:30.000Z (over 1 year ago)
- Last Synced: 2025-03-29T07:20:55.120Z (over 1 year ago)
- Language: TypeScript
- Size: 98.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# OpenAPI Example Code Generator
[](https://www.npmjs.com/package/openapi-example-code-generator)
[](https://opensource.org/licenses/MIT)
Generate executable code examples from OpenAPI specifications for multiple languages.
## Features
- **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
## Installation
```bash
npm install openapi-example-code-generator
# or
yarn add openapi-example-code-generator
```
## Usage
### Basic Example
```typescript
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();
```
### Advanced Usage
#### Custom Server URL
```typescript
// Override server URL from specification
const generator = new OpenAPICodeGenerator(
openApiSpec,
"https://custom-api.example.com"
);
```
#### Generate Specific Examples
```typescript
// Generate only Python examples
const pythonCode = generator.getPythonExamples();
// Generate Axios examples with TypeScript types
const axiosCode = generator.getAxiosExamples();
```
## Supported Languages
| 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(...)` |
## API Reference
### `new OpenAPICodeGenerator(spec: OpenAPIObject, serverUrl?: string)`
Create a new generator instance.
**Parameters:**
- `spec`: OpenAPI 3.0+ specification object
- `serverUrl`: Optional base URL override
### Methods
- `getCurlExamples(): string[]`
- `getPythonExamples(): string[]`
- `getJavaExamples(): string[]`
- `getJavaScriptExamples(): string[]`
- `getAxiosExamples(): string[]`
- `getAllExamples(): string[]`
## Contributing
Contributions are welcome! Please follow these steps:
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/your-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin feature/your-feature`)
5. Create a new Pull Request
## License
MIT © [zxypro1](https://github.com/zxypro1)