Making your APIs discoverable and self-documenting with the OpenAPI standard (Swagger)

When discussing Web APIs, you need to consider that the users that will interact with your API are developers, and not the end users of the product. The developers that use your API can use it in a variety of clients that can be written in various programming languages and technologies. Even though you created a REST API that uses standards such as HTTP and JSON, there are still things that are not standardized, such as the date-time format you use, numbers accuracy, and so on. Besides this, developers who want to use your API need a way to find out which data structures your API works with; what the URL structure is; what type of errors are returned from each action; and so forth. Then, the developer needs to put these definitions into code that can talk with your API, which can be a daunting task, especially when the API can change over time. You might even want to automate this task. To mitigate all the issues I've listed, the industry sought a standard specification that could be used to define the structure of an API in both a human and machine-readable way, which allows for the generation of documentation, client code, and UIs for interacting with the API in an automated way. This specification is known as OpenAPI (formerly, Swagger), and more information can be found at https://github.com/OAI/OpenAPI-Specification.

OpenAPI is a JSON document (usually with the name swagger.json) that contains metadata on the API (such as versions) and the description of each endpoint it exposes. The following example shows a reduced version of the GiveNTake API:

{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "GiveNTake.API"
},
"paths": {
"/api/Account/register": {
"post": {
"tags": [
"Account"
],
"operationId": "ApiAccountRegisterPost",
"consumes": [
"application/json-patch+json",
"application/json",
"text/json",
"application/*+json"
],
"produces": [],
"parameters": [
{
"name": "registration",
"in": "body",
"required": false,
"schema": {
"$ref": "#/definitions/RegisterUserDTO"
}
}
],
"responses": {
"200": {
"description": "Success"
}
}
}
},
...
},
"definitions": {
"RegisterUserDTO": {
"required": [
"email",
"password"
],
"type": "object",
"properties": {
"email": {
"type": "string"
},
"password": {
"format": "password",
"maxLength": 100,
"minLength": 6,
"type": "string"
},
"confirmPassword": {
"format": "password",
"type": "string"
}
}
},
...
},
"securityDefinitions": {
"Bearer": {
"name": "Authorization",
"in": "header",
"type": "apiKey"
}
},
"security": [
{
"Bearer": []
}
]
}

Swagger (https://swagger.io/) is not only the former name of the specification; it's also a set of open source tools that help in designing, validating, visualizing, and transforming the OpenAPI file and the REST API it represents.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.98.14