Sunshine PHP 2020
Daniel Abernathy
Austin, TX
"A broadly adopted industry standard for describing modern APIs."
A guide for developers on how to build or consume an API.
An agreement between all stakeholders on how an API's requests and responses should be structured.
Practical summary:
An OpenAPI document is a machine-readable description of an API's endpoints and the structure of the data in its requests and responses.
vs. API modeling tools like Postman
OpenAPI
Postman
vs. API format specifications like JSON:API
OpenAPI
JSON:API
vs. other API description specifications
API Blueprint
RAML
Documentation
Mock Server
Testing
Validation
(... and more)
{
"openapi": "3.0.0",
"info": {
"title": "Raffle API",
"version": "",
"description": "Raffle Application API"
},
"servers": [],
"paths": {},
"components": {}
}
"/widgets": {
"get": {
"description": "Get all widgets"
"responses": {
"200": {
"description": "OK"
}
}
}
}
Paths describe your individual endpoints
They primarily contain "operations", which correspond to HTTP verbs
"/widgets/{widgetId}": {
"parameters": [
{
"schema": {
"type": "integer"
},
"name": "widgetId",
"in": "path",
"required": true
}
]
}
Paths can also contain parameters
"patch": {
"description": "Update a widget",
"responses": {
"200": {
"description": "OK"
}
},
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}
}
The most important parts of an operation are the request body and the responses.
Data in OpenAPI is described as JSON Schema.... ish
OpenAPI uses an "extended subset" of an old draft of JSON Schema.
ಠ_ಠ
"schema": {
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
},
"description": {
"type": "string",
"nullable": true
}
}
}
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
}
}
}
}
}
"post": {
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
}
}
}
}
}
}
The root level components property lets you store data that you can reuse throughout document
{
"components": {
"schema": {},
"responses": {},
"parameters": {},
"requestBodies": {}
}
}
The $ref keyword lets you point to an item in the components section from elsewhere in the document.
{
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Widget"
}
}
}
git clone https://github.com/dabernathy89/openapi-workshop.git
cd openapi-workshop
git checkout start
composer install
npm install && npm run dev
touch database/database.sqlite
cp .env.example .env
php artisan key:generate
php artisan migrate --seed
cd public
php -S localhost:8080
GET /contests
POST /contests
GET /contests/{contestId}
PATCH /contests/{contestId}
DELETE /contests/{contestId}
GET /contests/{contestId}/prizes
POST /contests/{contestId}/prizes
GET /contests/{contestId}/contestants
POST /contests/{contestId}/contestants
PATCH /contestants/{contestantId}
DELETE /contestants/{contestantId}
PATCH /prizes/{prizeId}
DELETE /prizes/{prizeId}