Chapter 3. Designing and developing an API definition with API Designer
Chapter 3. Designing and developing an API definition with API Designer You can use API Designer to design and develop a REST API definition that complies with the OpenAPI 3 (or 2) specification. Prerequisites You created an OpenShift project. You added the API Designer service to your OpenShift project. 3.1. Creating a REST API definition in API Designer The following steps describe how to create a REST API definition. Note You can access the API Designer user interface from Fuse Online or Fuse on OpenShift. For Fuse on OpenShift only, API Designer is stateless which means that it does not save your work between OpenShift sessions. You need to save the API to your local file system between sessions. About the example The Task Management API example simulates a simple API that sales consultants might use to track the tasks that they need to do when interacting with customer contacts. Example "to-do" tasks might be "create an account for a new contact" or "place an order for an existing contact". To implement the Task Management API example, you create two paths - one for tasks and one for a specific task. You then define operations to create a task, retrieve all tasks or a specific task, update a task, and delete a task. Prerequisites You know the endpoints for the API that you want to create. For the Task Management API example, there are two endpoints: /todo and /todo/{id} . For Fuse on OpenShift only, you created an OpenShift project and you added the API Designer service to your OpenShift project. Procedure If you are using Fuse Online, skip to step 2. If you are using Fuse on OpenShift: Log in to your OpenShift web console and then open the project that contains API Designer. For OpenShift 4.x, select Topology and then click the URL link on the apicurito-service-ui icon. For OpenShift 3.11, from the list of applications, click the URL for API Designer, for example https://apidesigner-myproject.192.168.64.43.nip.io A new browser window or tab opens for API Designer. Note Because API Designer is a "light" version of the Apicurio Studio open source project , "Apicurio" shows in the API Designer interface. Click New API . A new API page opens. By default, API Designer uses the OpenAPI 3 specification. If you want to use the OpenAPI 2 specification, click the arrow to the New API button and then select OpenAPI 2 . Note If you open an API based on the OpenAPI 2 specification, you can use the API Designer's Convert to OpenAPI 3 option to convert the API to comply with the OpenAPI 3 specification. To change the API name: Hover the cursor over the name and then click the edit icon ( ) that appears. Edit the name. For example, type Task API . Click the checkmark icon to confirm the name change. Optionally: Provide a version number and a description. Add your contact information (name, email address, and URL). Select a license. Define tags. Define one or more servers. Configure a security scheme. Specify security requirements. Define a relative path to each individual endpoint of the API. The field name must begin with a slash (/). For the Task Management API example, create two paths: A path for tasks: /todo A path for a specific task by ID: /todo/{id} Specify the type of any path parameters. For the example id parameter: In the Paths list, click /todo/{id} . The id parameter appears in the PATH PARAMETERS section. Click Create . For the description, type: The ID of the task to find. For the type, select integer as 32-Bit integer . In the Data Types section, define reusable types for the API. Click Add a data type . In the Add Data Type dialog, type a name. For the Task Management API example, type Todo . Optionally, you can provide an example from which API Designer creates the data type's schema. You can then edit the generated schema. For the Task Management API example, start with the following JSON example: Optionally, you can choose to create a REST Resource with the data type. Click Save . If you provided an example, API Designer generates a schema from the example: Optionally, you can add edit the schema properties and add new ones. For the Task Management API example, create another data type named Task with one property named task of type string . For each path, define operations (GET, PUT, POST, DELETE, OPTIONS, HEAD, or PATCH). For the Task Management API example, define the operations as described in the following table: Table 3.1. Task Management API operations Path Operation Description Request Body Response /todo POST Create a new task. Media Type: application/json Data Type: Task Status Code: 201 Description: Task created Response Body: Media Type: application/json Data Type: Todo /todo GET Get all tasks. Not applicable Status Code: 200 Description: List of Tasks /todo/{id} GET Get a task by ID. Not applicable Status Code: 200 Description: Task found for ID Response Body: Media Type: application/json Data type: Task Status Code: 404 Description: No task with provided identifier found. /todo/{id} PUT Update a task by ID. Request Body type: Task Status Code: 200 Description: Completed Status Code: 400 Description: Task not updated /todo/{id} DELETE Delete a task by ID. Not applicable Status Code: 200 Description: Task deleted Status Code: 400 Description: Task not deleted Resolve any issues, as described in Resolving validation issues in API Designer . For Fuse on OpenShift only, save your API specification by clicking Save As and then select JSON or YAML format. The JSON or YAML file is downloaded to your local download folder. The default filename is openapi-spec with the appropriate file extension. Additional resources For information about the OpenAPI Specification, go to: https://github.com/OAI/OpenAPI-Specification 3.2. Resolving validation issues in API Designer When you create and edit an API, API Designer identifies issues that you must resolve with an exclamation (!) icon and also with a list of issues in the API Designer title bar. Prerequisites Open an API in API Designer. Procedure Find an issue indicated by an exclamation (!) icon. For example: Click the exclamation icon to view a description of the issue. For example: Based on the information provided by the issue description, navigate to the location of the issue and fix it. For example, to fix the "Operation must have at least one response" issue, click the GET operation to open it and then click Add a response . After you type a description for the response, the issue is resolved and the exclamation icon disappears: For a summary of all issues: Click the Issues link in the upper right corner. Click Go to a problem for a specific issue to go to the location of the issue so that you can resolve it.
{ "id": 1, "task": "my task", "completed": false }