A step by step how-to tutorial to create a simple OpenAPI 3.0 document for API integration.
About this task
zosConnect-3.0 Applies to zosConnect-3.0.
This tutorial provides a step by step how-to guide to create an OpenAPI 3.0 document for API integration to Db2® using the online Swagger Editor.
There is a video that goes with this tutorial and a ready to download completed copy of
EmployeesAPI for you to reference as you work through the steps.
The
Swagger Petstore - OpenAPI 3.0 sample loads. The API code editor
is in the left panel and the Swagger Editor is on the right. The Swagger
Editor displays a rendered version of the API defined in the code editor
panel on the left.
Figure 1. Swagger editor landing page
Click the left panel and clear the contents.
This tutorial creates a new API
called EmployeesAPI from scratch. When you clear the contents in the code view,
the Swagger Editor is updated to reflect no content in the file. See Figure 2.
Figure 2. Swagger editor with no content
Tip: It is useful to have the OpenAPI 3.0.0 specification open to reference https://swagger.io/specification/. Click
Specification and then Schema to view the format of
valid objects within an OpenAPI document. The
OpenAPI Object details the required fields as shown in Figure 3.
Figure 3. OpenAPI specification
and schema
Type openapi: 3.0.0 as the first line in the blank file.
openapi: 3.0.0
Note:OpenAPI v3.0.0 is the current version that
z/OS Connect supports.
Errors appear in the Swagger Editor as seen in Figure 4. These errors are resolved as
the OpenAPI 3.0 document is created with valid syntax.
Figure 4. Define an OpenAPI 3 document
Define the info object in the OpenAPI 3 definition by typing info:
on the next line.
The Info Object schema specification, as seen in Figure 5, states that title and
version are required fields that must be added to the info:
object. Figure 5. Info object fixed fields
Type the following to complete the info:
object.
info:
title: EmployeesApi
description: Employee Management API for Db2.
version: "0.1"
Note: The description field is optional. We add it here so people that use the
EmployeesAPI knows it purpose.
Define the servers object in the OpenAPI 3 definition by typing
servers: on the next line. The server object represents a server
by using the URL to the target host. Add a single URL to the server object with a
forward slash /.
servers:
- url: /
Note:IBM® z/OS Connect uses the
contextRoot to build the API. It is set to / and is
configurable.
Define the paths object in the OpenAPI 3 definition by typing paths:
on the next line.
The paths object holds the relative paths to the
individual endpoints and their operations. The path is appended to the URL from the
servers object in order to construct the full URL.
In this tutorial, we
want to query a list of employee records for use in a front-end application. To do this, start by
adding the employees path and then adding a get method on that
path with a description. Type the following in your OpenAPI 3 definition.
paths:
/employees:
get:
description: Uses the getEmployees Db2 z/OS asset.
Define the responses: object by typing responses: on
the next line.
The responses object is a container for the expected responses of an
operation that maps an HTTP response code to the expected response.
Note: The
responses: object must contain at least one response code that
should be the response code for a successful operation. The response code must be
in double quotation marks.
For example,
"200".
In this tutorial, a "200" response is
defined for a successful operation. Add a mandatory description.
responses:
"200":
description: OK
Define the content object by typing content: on the
next line and define the MIME type.
In this tutorial, the application is JSON. IBM z/OS Connect currently only supports the JSON
format.
content:
application/json:
Define the schema object for the "200" response by
typing schema: on the next line.
This tutorial receives a list of
items: as an array so the schema type is defined as an array. Each
item in the array has the same set of properties: as defined here:
employeeNumber with type string for example
"000150"
If you have no errors in the OpenAPI 3.0.0
definition, explore the API in the Swagger Editor as seen in Figure 7 and Figure 8.
Figure 7. New OpenAPI 3 document with no errors
To explore the API in the Swagger Editor, expand the operation.
In this tutorial, the operation is GET /employees that is a
combination of the method and the path as now defined in the
file.
Click as seen in Figure 7 to expand the GET /employeesoperation details.Figure 8. Expanded OpenAPI 3
Operation in the Swagger Editor
For the GET /employeesoperation, you can see.
responses: with a value of "200" for a successful
operation.
media type of application/json.
Data for each of the employees items returned in the array.
Tip: If you have errors, use the information in the Swagger Editor to
identify and resolve the errors. Figure 4
provides a view of how the errors are displayed.
What to do next
Learn how to enhance an OpenAPI 3.0 document
in the next tutorial, with some more advanced features and then review those features in the z/OS Connect Designer. For more information, see Enhancing a simple OpenAPI 3.0 document.