Phase 6 — Publish an API
Time to complete: ~20 minutes
Overview: In this phase, you’ll expose your workflow as REST APIs. The result will be two endpoints: one to fetch all SKUs and one to fetch a SKU by product code.
A view of the completed Phase 6 workflow.
A view of the completed Phase 6 API.
Steps
-
In this step, you will create a new workflow that will be used as the handler for an operation in your API. To create a workflow:
- Click the + button in the main toolbar and select Workflow.
- Browse to the Package
my-first-project
, then name the workflowMyApiWorkflow
and click Create.
-
In this step, you will add a Database Query node to your workflow. This node will query your local database and return a list of all SKUs. To add a Database Query node:
- Click the + icon in the workflow editor toolbar for the
MyApiWorkflow
workflow. - Drag the Database Query node onto the workflow editor canvas and drop it next to the Start Trigger. Then click the x icon to close the modal.
- If placed correctly, it should draw a line connecting the two nodes automatically. If not then simply connect the two nodes by dragging the output arrow of the Start Trigger to the input arrow of the Query database node.
- Click the expand icon on the Query database node to set the SQL statement that will be executed by this node. Enter the following parameters:
- Connection Name:
onboarding_01
- Type:
Select Multi
- Statement:
1
SELECT * FROM "SKU"
- Connection Name:
- In the same window, click the Data Mapping tab. Then right-click
inputCursor
in the Output and selectAdd Property and Map
. Name the PropertyskuArray
and click the Add Property and Map button. - Click Save in the main navigation toolbar to save your changes.
- Click the + icon in the workflow editor toolbar for the
-
In this step, you will create a REST API and expose an endpoint that will invoke the workflow from Step 1. To create a REST API:
- Click on the + icon in the main toolbar then select Create API.
- Enter a name for your API or select the default and click the Create button.
-
In this step, you will add a path to your REST API. A path is a unique public endpoint to your API. To create a new path for a REST API:
- Click on the Add Path icon in the API Editor toolbar.
- Enter the path
/skus
and click Finish.
-
In this step, you will add an operation to your REST API. An API operation specifies a HTTP method for interacting with the API services. A handler is associated each API operation. The handler contains the logic to be executed by the endpoint. To create an operation for a REST API:
- Click the Add Operation icon in the API Editor toolbar.
- Select the type
GET
and click Next. - Select the
MyApiWorkflow
workflow that you created in Step 1 and click Finish. This workflow will be the handler for this GET operation.
-
In this step, you will add a response to the operation that you created in Step 4. A response provides information about the outcome of the request, indicating whether it was successful, whether further action is required, or if an error occurred. To add a response code to the GET /skus operation:
- Under the GET operation for the path
/skus
right-click Responses then select Add Response. - Add the following parameters, then click Next:
- Status Code:
Default
- Description:
Get all SKUs
- Status Code:
- Select Use existing property and then select
skuArray
from the drop down menu. - Click Finish.
- Click Save in the main toolbar to Save your changes.
- Under the GET operation for the path
-
In this step you will invoke your API in a HTTP client to test the response. To invoke the operation in Martini's built-in HTTP client:
- Right click on the GET method for the path
/skus
then select Invoke in HTTP Client. The HTTP client will be automatically populated with the base URL and path of the operation. - Click Send to execute the request.
-
Expected output:
- Status:
200
- Body:
<a JSON array of SKUs>
The object below is a representative element of the list/array of SKUs returned:
1 2 3 4 5 6 7 8 9
{ "sku_id": 1, "name": "Chisel Wood 6mm", "productCode": "SKU001", "productVariant1": "Wood", "productVariant2": "6mm", "price": 13.0, "stockAvailable": 191 }
- Status:
- Right click on the GET method for the path