TABLE OF CONTENTS
- Step 1: Add the Form Component
- Step 2: Configure the External Data Source
- Step 3: Create the Data Sets
- Step 4: Add a Button Component
- Step 5: Configure Event Handlers
- Step 6: Add a Loading Indicator
- Step 7 (Optional): Display the Result
- Result
In this article, we’ll build a simple form to submit data to an API endpoint. This example demonstrates how to configure an App to collect user input, construct a request payload, and send it to an external API.
The workflow includes the following steps:
- Build a form for user input.
- Set up an external data source.
- Create data sets to hold and send data.
- Add a button to trigger submission.
- Configure event handlers to send the request.
- Add a loading indicator.
- (Optionally) Display the result.
You can download a ready-to-use example App configured with the steps described in the article. Import the API Request Example.zip file into App Studio, then create a Workspace to test the example App.
Step 1: Add the Form Component
The form collects the user input that will be passed to the API.
- Drag and drop the Form component onto the canvas.
- Rename it to waitForm.
- Adjust the following toggles:
Toggle | Value |
Submit on value change | ON |
Show primary button | OFF |
Show secondary button | OFF |
- Add a field:
Setting | Value |
Name | Time |
ID | time |
Type | Number |
This form will provide the Time value used in the payload. You’ll later map it as inputs.time in the JavaScript data set.
Step 2: Configure the External Data Source
The external data source defines where and how data is sent.
In this example, the endpoint accepts a time duration (in milliseconds), waits for that period, and then returns a response.
- Create a new external data source named apiLongTask.
- Use the following request configuration:
{
"url": "https://db-api.app.cafex.com/api/v2/app-studio-training/longTask/",
"method": "POST",
"payload": {
"fields": [
{
"name": "time",
"type": "number"
}
]
},
"requestHeaders": [{"name": "X-DataGateway-API-Key","value": "{{your_api_key_here}}"}]
}
Note: Replace {{your_api_key_here}} with your actual API key.
This configuration sends a POST request to the endpoint with the numeric field time.
Step 3: Create the Data Sets
Data sets define how information flows between your form and the external data source. In this step, we’ll create two data sets: a JavaScript data set to construct the payload and an External API data set to execute the request.
JavaScript Data Set
This JavaScript data set takes the Time value from the form and wraps it in an object to use as the API request body.
- Go to App > Configuration.
- Under Data sets, click Add data set.
Setting | Value |
Name | payload |
Type | JavaScript |
|
|
Name | time |
Data set | waitForm |
Data path | Time |
Required | ON |
|
|
Output data type | Object |
Add a field: | Number |
Name | time |
|
return { "time": inputs.time ?? "2000" };
|
If no value is entered in the form, it defaults to 2000 milliseconds, so the request always contains a valid time value.
External API Data Set
This data set executes the API call.
- Go to App > Configuration.
- Under Data sets, click Add data set.
Setting | Value |
Name | apiLongTask |
Type | External API |
Source | apiLongTask (the data source from Step 2) |
Step 4: Add a Button Component
In this step, we’ll add a Button to trigger the API request.
- Drag and drop the Button component onto the canvas.
- For Text, enter Start Task.
- Configure event handlers, as described in Step 5.
Step 5: Configure Event Handlers
The event handlers define what happens when the button is clicked. In this step, we'll configure two event handlers: the first one evaluates the payload, and the second one sends the API request.
Evaluate Payload Event Handler
This event handler prepares the request body by evaluating the JavaScript data set.
- Select the Button.
- Under Configuration > Event handlers, click New event handler.
- Specify the following:
Setting | Value |
Handler name | Evaluate Payload |
Event type | Click |
Action | Evaluate a data set |
Data set to evaluate | payload |
Wait for response before continuing | ON |
Continue on error | ON |
Send API Request Event Handler
This event handler sends the evaluated payload to the external API.
- Select the Button.
- Under Configuration > Event handlers, click New event handler.
- Specify the following:
Setting | Value |
Handler name | Send API Request |
Event type | Click |
Action | Evaluate a data set |
Data set to evaluate | apiLongTask |
Payload data set | Payload |
Wait for response before continuing | ON |
Continue on Error | ON |
These two event handlers ensure that the App first prepares the payload from the form input, and then sends the completed request to the endpoint.
Step 6: Add a Loading Indicator
The Loading indicator displays the loading message when the API call is in progress.
- Drag and drop the Container component onto the canvas.
- In Appearance > Visibility, enter:
{{apiLongTask:evaluating}} - Drag and drop the Text component inside the container.
- For Configuration > Text, enter:
Loading…
This setup displays the loading message only while the API request is being processed.
Step 7 (Optional): Display the Result
To indicate that the request has completed:
- Drag and drop the Text component onto the canvas.
- For Configuration > Text, enter:
Request completed: {{ apiLongTask.responseTime}}ms - For Appearance > Visible, specify the mustache expression:
{{ apiLongTask }}
This shows the message once the API request has finished and returned a response.
Result
When you enter a value in the Time field (for example, 5000 for 5 seconds) and click Start Task:
- The App sends the payload { "time": 5000 } to the endpoint.
- The API waits 5 seconds before responding.
- The Loading... message appears during that time.
- When complete, the Request completed message is displayed.
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article