Start an API Request
Datasource
The easiest way to perform an API Request is to use the Datasource feature.
- Create a Datasource using an already created API Request
- Bind the request parameter values and the responses.
- Enable the Auto start when Screen First Loads or add an Start a Datasource request Event Action.
Example with steps
- Add a List View.
- Open Datasource.
- Press + Create Datasource.
- Select External API tab.
- Select the Weather API and then the Get Cities API Request.
- Bind API Request Parameters. (There is only one in this example)
- Press the + on the country parameter.
- Bind parameter with a the
Cy
Static Value. You can use any other supported option. - Press SAVE
- Bind the Response parameters. (There is only one in this example)
- Select the Cities Fetched Response.
- Press the + on the cities response parameter.
- Select the Design Element option, if not already selected.
- Select the List View from step 1.
- Add a new Cell, called City Cell.
- Select the newly created City Cell.
- Select if you want to edit the Cell. If you are reusing a previously edited cell, you don’t need to edit it again.
- Press SAVE to close the Datasource editor.
- Select if you want to Auto Start the Datasource when Screen First Loads.
Code Block
You can perform an API Request using the Execute API Code Block.
- Create an API Request.
- Open a Code Blocks editor. You can either create or edit an existing Function.
- Add the Execute API Request Code Block.
- Select the API Request.
- Set self block as the delegate parameter.
- Set the parameter inputs, if any.
- Close Code Blocks editor.
- Implement API Response protocols.
- Go to Protocols Tab.
- Press the API-
Name Of Your API
- Add and edit the required Functions.
Note: After you save your Function, Protocols Tab will display the Protocols that you need to implement to handle the API Responses. Every API Response will call the respective Function.
- Add the Execute a Function Event Action to an Event, for example, the Button On Click Event.
Example with steps
- Add a List View.
- Create a new Screen property, called cities.
- Go to the Data Inspector. (5th tab on the Inspectors section)
- Press the + to add a new Screen property.
- Select the already created City Model.
- Select the List(array) option, as the API Response that we will use returns multiple cities.
- (Optional) Edit the name of the new screen property.
- Press SAVE
- Bind the cities Screen property to the List View. By binding a Screen property to a Design Element, every time the value of the Screen property is updated, the Design Element_(List View)_ will reload with the new Data.
- Select the Cell that will be used. Note: Cell needs to have one Cell property with the City Data Type.
- Go to the Code section.
- Add a new Function, called call get cities API.
- Implement the Execute API Code Block.
- Drag the Code Block from the Blocks->API.
- Select the API Request from the dropdown.
- Set self block as the delegate parameter.
- Set the country parameter with a Static Text Code Block and type CY.
- Close Function
- Implement Response protocols.
- Select the Protocols Tab.
- Select the API-Get Cities.
- Press Add on the Cities Fetched suggested Function.
- Edit the Cities Fetched function.
- Pass the value of the fetched cities to the cities property from step 2. Note: API Response functions automatically create Function arguments with all API Response parameters’ values.
- Drag the Setter Block for the cities properties.
- Connect it to the cities Function Argument.
- Close the Function.
- Implement a way to execute the function. For example, add the Execute a Function Event Action to an Button On Click Event.