Exploring an OpenAPI document
This tutorial covers the basics of the OpenAPI specification and guides you through writing an OpenAPI document that describes a service API. We’ll use a fictitious service that returns a personalized greeting.
% curl 'localhost:8080/api/greet?name=Jane'
{
"message" : "Hello, Jane"
}
Explore the GreetingService OpenAPI document
The OpenAPI document in openapi.yaml
provides a structured declaration of the service.
Every OpenAPI document needs to declare its format version using the
openapi
key. Use the version3.1.0
for this document.openapi.yaml
exploring-openapi.openapi.0.yamlopenapi: '3.1.0'
The
info
section contains the service name,GreetingService
, and declares the API version.openapi.yaml
exploring-openapi.openapi.1.yamlopenapi: '3.1.0' info: title: GreetingService version: 1.0.0
The
servers
section declares a list of existing service URLs.openapi.yaml
exploring-openapi.openapi.2.yamlopenapi: '3.1.0' info: title: GreetingService version: 1.0.0 servers: - url: https://example.com/api description: Example service deployment.
The
paths
section contains the list of paths and HTTP operations that make up the API.There is a single path
/greet
on which the server will handle HTTPGET
requests. Provide anoperationId
to give code generators a useful hint of what to call the generated method.openapi.yaml
exploring-openapi.openapi.3.yamlopenapi: '3.1.0' info: title: GreetingService version: 1.0.0 servers: - url: https://example.com/api description: Example service deployment. paths: /greet: get: operationId: getGreeting
Document the optional query parameter that provides the name of the person to be greeted in the
parameters
section.openapi.yaml
exploring-openapi.openapi.4.yamlopenapi: '3.1.0' info: title: GreetingService version: 1.0.0 servers: - url: https://example.com/api description: Example service deployment. paths: /greet: get: operationId: getGreeting parameters: - name: name required: false in: query description: The name used in the returned greeting. schema: type: string
The different responses are also documented. In this case, the server will always return the HTTP status code
200
.openapi.yaml
exploring-openapi.openapi.5.yamlopenapi: '3.1.0' info: title: GreetingService version: 1.0.0 servers: - url: https://example.com/api description: Example service deployment. paths: /greet: get: operationId: getGreeting parameters: - name: name required: false in: query description: The name used in the returned greeting. schema: type: string responses: '200': description: A success response with a greeting.
In the response, describe the structure of the JSON body using JSON Schema.
Define the reusable type in the
#/components/schemas
section, give it the nameGreeting
, and refer to it from the response definition of the JSON response body.openapi.yaml
exploring-openapi.openapi.6.yamlopenapi: '3.1.0' info: title: GreetingService version: 1.0.0 servers: - url: https://example.com/api description: Example service deployment. paths: /greet: get: operationId: getGreeting parameters: - name: name required: false in: query description: The name used in the returned greeting. schema: type: string responses: '200': description: A success response with a greeting. content: application/json: schema: $ref: '#/components/schemas/Greeting' components: schemas: Greeting: type: object properties: message: type: string required: - message
There you go! This is a simple, yet complete, example of an OpenAPI document that describes the API of
GreetingService
.openapi.yaml
exploring-openapi.openapi.7.yamlopenapi: '3.1.0' info: title: GreetingService version: 1.0.0 servers: - url: https://example.com/api description: Example service deployment. paths: /greet: get: operationId: getGreeting parameters: - name: name required: false in: query description: The name used in the returned greeting. schema: type: string responses: '200': description: A success response with a greeting. content: application/json: schema: $ref: '#/components/schemas/Greeting' components: schemas: Greeting: type: object properties: message: type: string required: - message