Introduction to OpenAPI Specification

A presentation at PHP[TEK] in May 2019 in Atlanta, GA, USA by Lorna Jane Mitchell

Slide 1

Slide 1

Leveraging API Specification Languages Lorna Mitchell, Nexmo

Slide 2

Slide 2

Describing APIs • Describe RESTful HTTP APIs in a machine-readable way Then the machine can read it and write: • API reference documentation • Code SDKs • Postman collections • Mock servers @lornajane

Slide 3

Slide 3

API Spec Languages • OpenAPI is an open standard, and is used in this talk • RAML from Mulesoft (who also now support OpenAPI) • API Blueprint from Apiary @lornajane

Slide 4

Slide 4

Spec-First API Design @lornajane

Slide 5

Slide 5

New APIs or Existing Ones? @lornajane

Slide 6

Slide 6

New APIs or Existing Ones? Yes! @lornajane

Slide 7

Slide 7

Anatomy of OpenAPI Spec @lornajane

Slide 8

Slide 8

Anatomy of OpenAPI Spec Top-level elements: • openapi • info • servers • paths • components • security • tags @lornajane

Slide 9

Slide 9

OpenAPI Spec Examples A JSON or YAML file holds the description (this is YAML) openapi: 3.0.0 servers: - url: ‘https://api.nexmo.com/ni’ info: title: Number Insight API version: 1.0.3 description: >Nexmo’s Number Insight API delivers real-time intelligence about the validity … a few hundred more lines here @lornajane

Slide 10

Slide 10

Documenting an Endpoint paths: ‘/basic/{format}’: parameters: - $ref: ‘#/components/parameters/format’ get: operationId: getNumberInsightBasic parameters: - $ref: ‘#/components/parameters/number’ - $ref: ‘#/components/parameters/country’ responses: ‘200’: description: OK content: application/json: schema: $ref: ‘#/components/schemas/niResponseJsonBasic’ @lornajane

Slide 11

Slide 11

Reference Parameters components: parameters: number: name: number in: query description: ‘A single phone number that you need insight about in national example: ‘447700900000’ required: true country: name: country in: query example: ‘GB’ description: ‘If a number does not have a country code or is uncertain, set schema: type: string pattern: ‘[A-Z]{2}’ @lornajane

Slide 12

Slide 12

That looks complicated! @lornajane

Slide 13

Slide 13

Editing Tools There are editors with helpful tools • Stoplight is highly recommended if you like a web interface https://stoplight.io • I like Atom with linter-swagger https://atom.io • Try SwaggerUI, SwaggerHub, etc https://swagger.io/tools/ • APICurio Studio gets good reviews https://www.apicur.io/ @lornajane

Slide 14

Slide 14

Please use Source Control See also: https://gitworkbook.com @lornajane

Slide 15

Slide 15

Validation Tools Tools that check or “lint” your file. • Speccy is a CLI tool with configurable rules http://speccy.io/ • Spectral is a newer CLI tool, also with rules https://github.com/stoplightio/spectral • Open API Spec Validator https://github.com/p1c2u/openapi-spec-validator @lornajane

Slide 16

Slide 16

Preview Tools OAS is a standard! So any preview should do: • ReDoc is great https://github.com/Rebilly/ReDoc • You can run OpenApi-GUI locally https://github.com/mermade/openapi-gui • Nexmo uses a custom renderer https://developer.nexmo.com/api • Stoplight shows documentation as you go (more on this later) https://stoplight.io @lornajane

Slide 17

Slide 17

Docs Example: ReDoc @lornajane

Slide 18

Slide 18

Docs Example: Nexmo @lornajane

Slide 19

Slide 19

Docs Example: Stoplight @lornajane

Slide 20

Slide 20

Stoplight Online Editor @lornajane

Slide 21

Slide 21

Stoplight Design View @lornajane

Slide 22

Slide 22

Imported into Postman @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

Creating OpenAPI Specs is like eating an elephant @lornajane

Slide 26

Slide 26

Uses for OpenAPI Spec @lornajane

Slide 27

Slide 27

Resources • https://www.openapis.org • https://openapi.tools • https://apievangelist.com • https://stoplight.io • https://speccy.io • https://github.com/Rebilly/ReDoc • https://github.com/openapitools/openapi-generator @lornajane