Create a REST API model
In the previous tutorial, we reviewed how to create data models for property graph databases. By the end of this tutorial, you will master how to create a model for REST APIs using Swagger or OpenAPI specification.
You may also view this tutorial on YouTube. Summary slides can be found here.
While it is useful to be familiar with REST APIs in general, and the Swagger/OpenAPI specification in particular, the great advantage of Hackolade Studio's handling of REST APIs documentation is that the user does not have to know the syntax for Swagger or OpenAPI. You create your API with the user interface, and the application takes care of generating the proper syntax, thereby avoiding costly mistakes, and increasing productivity and API quality.
Hackolade takes a visual and schema-centric approach to building REST APIs, and fits well in API-first and design-first processes. It can also help understand existing APIs with a visual representation of Swagger or OpenAPI files, after reverse-engineering of such files, whether in JSON or YAML.
Given the rise of OpenAPI version 3, replacing the previous Swagger version 2 specification, the rest of this tutorial will focus on OpenAPI. Most of the principles below apply also to Swagger, even if some implementation features may vary.
For the purpose of this tutorial, we will build a very simple API for an URL shortener, i.e. a service letting:
- a user create an entry based on a full URL, and receive a short slug generated by the service;
- any system send a slug and get back the full URL.
The example is inspired by Shorty, an existing service, but the OpenAPI documentation is enhanced to demonstrate best practices and how to leverage the features of Hackolade Studio. By the end of the tutorial, you will have created a visual model for your API:
and generated the corresponding OpenAPI file:
Here is the link if you want to download this data model.
Design the API
An API is an interface for people or systems to consume a service. It is critical to put yourself in the shoes of the consumer, and make the API easy to understand and navigate the service. It must be usable, valuable, credible, and useful. Good design and good documentation go a long way towards adoption, along with collaboration and communication.
The sequence diagram for the first process to create an entry based on a full URL and receive a short slug generated by the service can be drawn as follows:
The sequence diagram for the second process to send a slug and get back the full URL is as follows:
Typically, Swagger tutorials teach you to build an API in the order of sections in an OpenAPI file: first the info section, then the paths, and finish with components. That would be like wanting to paint the house before the foundations are built... We prefer to start with what matters most, the core foundation of the API: the schema describing the structure of the service contract being exchanged between the producer and the consumer.
Accordingly, the process to create this API is as follows:
-
create reusable components
-
a schema for the exchange
-
a request body to submit a long URL, using the schema created in the previous step
-
a simple parameter to fetch the full URL from a previously generated slug
-
a couple of responses
- returning the generated slug (200 - OK) using the common schema
- returning an error message, reusable for different kinds of errors
-
an OAuth2 security scheme (could also be done at a later stage)
-
-
create resources with requests and responses
-
add endpoint, tags, and metadata information, plus color coding of responses
-
generate the OpenAPI documentation file, with API testing
Build the API
We will now use Hackolade Studio to build the API step by step. Let's create a new model and choose OpenAPI as a target. Remember that the application will automatically generate the OpenAPI file with proper syntax, as you build the API in the screens. You may check the progress at any time in the lower tab OpenAPI File.
Create reusable components
Often, multiple API operations have some common parameters or return the same response structure. To avoid code duplication and facilitate evolution, you can place the common definitions in the global components section and reference them using $ref. In Hackolade, components are handled like model references, and are maintained in the lower tab Components. If a component evolves, these changes are automatically reflected in all places where the component is referenced.
Schema components for the exchange
For this very simple service, we just need 2 schemas, one for the happy flow exchange, and one for error messages. Note that the url schema can be used both in the creation request and the 200 response. The url schema has just 2 fields: longURL and slug, both of string data type. And the error schema is also real simple with a status code according to the RFC 7231 standard, and a text message.
The steps to create a schema in Hackolade have been covered in an earlier tutorial in section "Hierarchical schema view".
If you already have complex schemas defined in other Hackolade target data models (or JSON Schema files, or a Hackolade Polyglot model, or in other OpenAPI files, or on SchemaHub), you may reference any of those as external references so they can be kept in sync with their master version for higher data quality and consistency.
Request body to submit a long URL
To create a request body in components, you do a right-click on the requestBodies box and get a contextual menu where you're guided to choose the appropriate option:
A template is created to guide you through the creation, with a standard media type application/json and its structure. You may create additional media types, from a list of popular ones, or by adding your own if needed:
In our case, we will rename the requestBody to urlBody and replace the schema object by a reference to the schema component created earlier.