Choose the command matching your framework below. After extraction, always validate with speakeasy lint.
Python: FastAPI
FastAPI generates an OpenAPI schema at runtime. Export it without starting the server:
```bash
python -c "import json; from myapp import app; print(json.dumps(app.openapi()))" > openapi.json
```
Replace myapp with the module containing your FastAPI app instance. If the app uses a factory pattern:
```bash
python -c "import json; from myapp import create_app; app = create_app(); print(json.dumps(app.openapi()))" > openapi.json
```
You can also start the server and fetch from http://localhost:8000/openapi.json.
Python: Flask (flask-smorest)
Requires [flask-smorest](https://flask-smorest.readthedocs.io/) or [apispec](https://apispec.readthedocs.io/):
```bash
flask openapi write openapi.json
```
If using apispec directly, export programmatically:
```python
import json
from myapp import create_app, spec
app = create_app()
with app.app_context():
print(json.dumps(spec.to_dict()))
```
Python: Django REST Framework
Requires [drf-spectacular](https://drf-spectacular.readthedocs.io/):
```bash
python manage.py spectacular --file openapi.yaml
```
For JSON output:
```bash
python manage.py spectacular --format openapi-json --file openapi.json
```
Java: Spring Boot
Requires [springdoc-openapi](https://springdoc.org/). Start the application, then fetch the spec:
```bash
# Start the app (background)
./mvnw spring-boot:run &
# Wait for startup
sleep 15
# Fetch the spec
curl http://localhost:8080/v3/api-docs -o openapi.json
# For YAML format
curl http://localhost:8080/v3/api-docs.yaml -o openapi.yaml
# Stop the app
kill %1
```
If the server runs on a different port or context path, adjust the URL accordingly.
TypeScript: NestJS
Requires [@nestjs/swagger](https://docs.nestjs.com/openapi/introduction). Start the application, then fetch:
```bash
# Start the app (background)
npm run start &
sleep 10
# Fetch the spec (default path with SwaggerModule)
curl http://localhost:3000/api-json -o openapi.json
# Stop the app
kill %1
```
Alternatively, create a script to export without running the server:
```typescript
// scripts/export-openapi.ts
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from '../src/app.module';
import * as fs from 'fs';
async function bootstrap() {
const app = await NestFactory.create(AppModule, { logger: false });
const config = new DocumentBuilder().setTitle('API').build();
const doc = SwaggerModule.createDocument(app, config);
fs.writeFileSync('openapi.json', JSON.stringify(doc, null, 2));
await app.close();
}
bootstrap();
```
TypeScript: Hono (zod-openapi)
Requires [@hono/zod-openapi](https://github.com/honojs/middleware/tree/main/packages/zod-openapi). Export the schema programmatically:
```typescript
// scripts/export-openapi.ts
import { app } from '../src/app';
import * as fs from 'fs';
const doc = app.doc('/doc', {
openapi: '3.1.0',
info: { title: 'API', version: '1.0.0' },
});
fs.writeFileSync('openapi.json', JSON.stringify(doc, null, 2));
```
Run with:
```bash
npx tsx scripts/export-openapi.ts
```
Ruby: Rails (rswag)
Requires [rswag](https://github.com/rswag/rswag):
```bash
rails rswag:specs:swaggerize
```
The spec is written to swagger/v1/swagger.yaml by default (configurable in config/initializers/rswag_api.rb).
PHP: Laravel (l5-swagger)
Requires [l5-swagger](https://github.com/DarkaOnLine/L5-Swagger):
```bash
php artisan l5-swagger:generate
```
The spec is written to storage/api-docs/api-docs.json by default.