API Dokumentation OpenAPI
OpenAPI/Swagger: API-Dokumentation
OpenAPI ist der Standard für REST-API-Dokumentation. Lernen Sie, wie Sie Ihre API professionell dokumentieren.
Grundstruktur
# openapi.yaml
openapi: 3.0.3
info:
title: My API
description: API für meine Anwendung
version: 1.0.0
contact:
email: api@example.com
servers:
- url: https://api.example.com/v1
description: Production
- url: https://staging-api.example.com/v1
description: Staging
paths:
/users:
get:
# ...
post:
# ...
components:
schemas:
User:
# ...
securitySchemes:
bearerAuth:
# ...
Paths und Operations
paths:
/users:
get:
summary: Liste aller Benutzer
description: Gibt eine paginierte Liste aller Benutzer zurück
operationId: getUsers
tags:
- Users
parameters:
- name: page
in: query
description: Seitennummer
schema:
type: integer
default: 1
- name: limit
in: query
description: Einträge pro Seite
schema:
type: integer
default: 20
maximum: 100
responses:
'200':
description: Erfolgreiche Antwort
content:
application/json:
schema:
type: object
properties:
data:
type: array
items:
$ref: '#/components/schemas/User'
pagination:
$ref: '#/components/schemas/Pagination'
'401':
$ref: '#/components/responses/Unauthorized'
post:
summary: Benutzer erstellen
operationId: createUser
tags:
- Users
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/CreateUserRequest'
responses:
'201':
description: Benutzer erstellt
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'400':
$ref: '#/components/responses/ValidationError'
/users/{id}:
get:
summary: Benutzer abrufen
operationId: getUser
tags:
- Users
parameters:
- name: id
in: path
required: true
schema:
type: integer
responses:
'200':
description: Erfolg
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'404':
$ref: '#/components/responses/NotFound'
Schemas (Datenmodelle)
components:
schemas:
User:
type: object
required:
- id
- email
- name
properties:
id:
type: integer
example: 123
email:
type: string
format: email
example: user@example.com
name:
type: string
minLength: 2
maxLength: 100
example: Max Mustermann
role:
type: string
enum: [user, admin, moderator]
default: user
createdAt:
type: string
format: date-time
profile:
$ref: '#/components/schemas/UserProfile'
CreateUserRequest:
type: object
required:
- email
- name
- password
properties:
email:
type: string
format: email
name:
type: string
minLength: 2
password:
type: string
minLength: 8
format: password
Pagination:
type: object
properties:
page:
type: integer
limit:
type: integer
total:
type: integer
totalPages:
type: integer
Authentifizierung
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
apiKey:
type: apiKey
in: header
name: X-API-Key
oauth2:
type: oauth2
flows:
authorizationCode:
authorizationUrl: https://auth.example.com/authorize
tokenUrl: https://auth.example.com/token
scopes:
read: Lesezugriff
write: Schreibzugriff
# Global anwenden
security:
- bearerAuth: []
# Oder pro Operation
paths:
/public:
get:
security: [] # Keine Auth
/private:
get:
security:
- bearerAuth: []
Fehler-Responses
components:
responses:
NotFound:
description: Ressource nicht gefunden
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
example:
error:
code: NOT_FOUND
message: Die angeforderte Ressource wurde nicht gefunden
ValidationError:
description: Validierungsfehler
content:
application/json:
schema:
type: object
properties:
error:
type: object
properties:
code:
type: string
example: VALIDATION_ERROR
message:
type: string
details:
type: array
items:
type: object
properties:
field:
type: string
message:
type: string
Unauthorized:
description: Nicht authentifiziert
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
schemas:
Error:
type: object
properties:
error:
type: object
properties:
code:
type: string
message:
type: string
Tools
# Swagger UI (Interaktive Dokumentation) docker run -p 8080:8080 \ -e SWAGGER_JSON=/spec/openapi.yaml \ -v ./openapi.yaml:/spec/openapi.yaml \ swaggerapi/swagger-ui # Swagger Editor (online) # editor.swagger.io # Code-Generierung npx @openapitools/openapi-generator-cli generate \ -i openapi.yaml \ -g typescript-fetch \ -o ./generated-client # Validierung npx @redocly/cli lint openapi.yaml # Redoc (Alternative zu Swagger UI) npx @redocly/cli build-docs openapi.yaml
Aus Code generieren
// Node.js: swagger-jsdoc
/**
* @openapi
* /users:
* get:
* summary: Liste aller Benutzer
* responses:
* 200:
* description: Erfolg
*/
app.get('/users', getUsers);
// PHP: zircote/swagger-php
/**
* @OA\Get(
* path="/users",
* summary="Liste aller Benutzer",
* @OA\Response(response="200", description="Erfolg")
* )
*/
public function getUsers() {}
💡 Tipp:
Halten Sie Ihre OpenAPI-Spezifikation aktuell. Automatisieren Sie die Validierung in Ihrer CI/CD-Pipeline.