Introduction to OpenAPI

A presentation at GOTO Copenhagen in November 2019 in Copenhagen, Denmark by Lorna Jane Mitchell

Slide 1

Slide 1

Introduction to OpenAPI Lorna Mitchell, Nexmo

Slide 2

Slide 2

APIs are the engine of modern software development API descriptions power up our API workflows @lornajane

Slide 3

Slide 3

Spec-First API Design @lornajane

Slide 4

Slide 4

New APIs or Existing Ones? @lornajane

Slide 5

Slide 5

New APIs or Existing Ones? Yes! @lornajane

Slide 6

Slide 6

API Description Languages • API Blueprint from Apiary • RAML from Mulesoft (who also now support OpenAPI) • OpenAPI is an open standard, and is used in this talk • “The standard formerly known as Swagger” @lornajane

Slide 7

Slide 7

OpenAPI Descriptions WARNING may contain YAML @lornajane

Slide 8

Slide 8

MetaData openapi: 3.0.0 servers: - url: ‘https://api.nexmo.com/ni’ info: title: Number Insight API version: 1.0.4 description: Nexmo’s Number Insight API delivers real-time intelligence … contact: name: Nexmo DevRel email: devrel@nexmo.com url: ‘https://developer.nexmo.com/’ termsOfService: ‘https://www.nexmo.com/terms-of-use’ license: name: ‘The MIT License (MIT)’ url: ‘https://opensource.org/licenses/MIT’ @lornajane

Slide 9

Slide 9

Endpoints in OpenAPI Spec paths: ‘/basic/{format}’: get: operationId: getNumberInsightBasic ‘/standard/{format}’: get: operationId: getNumberInsightStandard ‘/advanced/async/{format}’: get: operationId: getNumberInsightAsync ‘/advanced/{format}’: get: operationId: getNumberInsightAdvanced @lornajane

Slide 10

Slide 10

Parameters and Responses parameters: - $ref: ‘#/components/parameters/format’ - $ref: ‘#/components/parameters/number’ - $ref: ‘#/components/parameters/country’ responses: ‘200’: description: OK content: application/json: schema: $ref: ‘#/components/schemas/niResponseJsonBasic’ text/xml: schema: $ref: ‘#/components/schemas/niResponseXmlBasic’ @lornajane

Slide 11

Slide 11

OpenAPI $ref Feature (Re)Use any block. For example the format parameter: components: parameters: format: name: format in: path required: true description: ‘The format of the response’ example: json schema: type: string enum: - ‘json’ - ‘xml’ @lornajane

Slide 12

Slide 12

That’s a lot of YAML … @lornajane

Slide 13

Slide 13

How to Edit OpenAPI Specs Option 1: your usual editor (this is https://atom.io) @lornajane

Slide 14

Slide 14

Text-Based API Descriptions @lornajane

Slide 15

Slide 15

How to Edit OpenAPI Specs Option 2: specialist tool https://stoplight.io/studio/ @lornajane

Slide 16

Slide 16

Validate and Check Style Spectral https://github.com/stoplightio/spectral Pick-and-mix the rules, and sprinkle in a few of your own! @lornajane

Slide 17

Slide 17

Even the machines can understand … now what? @lornajane

Slide 18

Slide 18

Generate Documentation Use your OpenAPI spec as the source for your API reference documentation • A choice of tools • Separate content and presentation • The spec supports and encourages examples @lornajane

Slide 19

Slide 19

Docs Example: ReDoc @lornajane

Slide 20

Slide 20

Docs Example: Nexmo @lornajane

Slide 21

Slide 21

Explore APIs with Postman @lornajane

Slide 22

Slide 22

OpenAPI Spec To Mock Server Prism is a mock server https://stoplight.io/prism/ @lornajane

Slide 23

Slide 23

Generated Code Libraries This example is from OpenAPI Generator https://github.com/OpenAPITools/openapi-generator docker run —rm -v ${PWD}:/local \ openapitools/openapi-generator-cli generate -i number-insight.yml -g php -o /local/out/php @lornajane

Slide 24

Slide 24

Generated Code Libraries To use it: 1 2 3 4 5 6 7 8 9 10 11 12 13 require_once(‘out/php/vendor/autoload.php’); // copy code from README, set API key and secret $apiInstance = new OpenAPI\Client\Api\DefaultApi( new GuzzleHttp\Client(), $config); $format = “json”; $number = “447700900000”; try { $result = $apiInstance->getNumberInsightBasic($format, $number); print_r($result); } catch (Exception $e) { echo ‘Exception when calling DefaultApi->getNumberInsightBasic’; } @lornajane

Slide 25

Slide 25

Finding Tools https://openapi.tools - a community listing @lornajane

Slide 26

Slide 26

OpenAPI: Brave New World @lornajane

Slide 27

Slide 27

Resources • https://developer.nexmo.com • https://openapis.org • https://stoplight.io (Studio, Prism and Spectral) • https://github.com/Nexmo/nexmo-oas-renderer • https://github.com/Redocly/redoc • https://openapi.tools @lornajane

Slide 28

Slide 28

@lornajane