Using Loops to Filter Data
This tutorial will guide you through using loops in Kinesis API to filter a list of data. You'll create an API endpoint that fetches a list of creatures and returns only the ones that match a specific criterion passed in the URL. This is a great way to learn how to build more dynamic and powerful API logic.
Prefer video tutorials? You can follow along with our YouTube walkthrough of this same project.
Prerequisites
Before you begin, you need:
- Access to a Kinesis API instance:
- Either the demo environment at demo.kinesis.world
- Or your own self-hosted instance
- A user account with ADMIN or ROOT privileges
- Basic understanding of REST APIs and the Flow Editor
1. Creating a Project
First, let's create a project for our creatures API:
- Log in to your Kinesis API instance
- Navigate to the Projects page from the main menu
- Click "Create a new project" to open the project creation modal
- Fill in the following details:
- Name: "Creatures"
- ID: "creatures"
- Description: "To have fun with some beings." (or anything else you want)
- API Path: "/creatures" (this will be the base URL path for all routes in this project)
- Click "Create" to save your project
2. Creating a Collection
Next, we'll create a collection to store our creature data:
- From your newly created project page, click "Create New" on the "Collections" section
- Fill in the following details:
- Name: "Creatures"
- ID: "creatures"
- Description: "To store details on every creature." (or anything else you want)
- Click "Create" to save your collection
3. Creating Structures
Now we'll create some structures (fields) to store our creature value:
- From the "Creatures" collection page, locate the "Structures" section
- Click "Create New" to add a structure
- Fill in the following details:
- Name: "Species"
- ID: "species"
- Description: "" (leave blank or insert anything else)
- Type: "TEXT"
- Required: Check this box
- Unique: Check this box
- Click "Create" to save the structure
- Click "Create New" to add another structure
- Fill in the following details:
- Name: "Is a pet"
- ID: "is_pet"
- Description: "Whether the species is considered as a pet." (leave blank or insert anything else)
- Type: "BOOLEAN"
- Required: Check this box
- Click "Create" to save the structure
4. Creating Data
Now we'll create some initial data objects for our creatures:
- Navigate to the "Data" section in the top navigation bar
- Select your project and then the "Creatures" collection
- Click "Create New" to create a data object
- Add a nickname (optional)
- For the "species" field, enter: "Dog"
- For the "is a pet" field, check the checkbox
- Click "Create" to save your data object
- Repeat the above steps as many times as you want for different species and whether they are typically considered to be pets
- Those are the data objects that have been created as example for this tutorial:
- Creature 1:
- species: "Dog"
- is_pet:
true
- Creature 2:
- species: "Cat"
- is_pet:
true
- Creature 3:
- species: "Scorpion"
- is_pet:
false
- Creature 4:
- species: "Kangaroo"
- is_pet:
false
- Creature 5:
- species: "Hamster"
- is_pet:
true
- Creature 1:
5. Creating a Route
Now, we'll create a route that filters these creatures based on whether they are a pet:
- Navigate to Routes in the top navigation bar
- Select your project
- Click "Create New" to create a route
- Fill in the route details:
- Route ID: "fetch_creatures"
- Route Path: "/fetch"
- HTTP Method: "GET"
- In the URL Parameters section, set the delimiter to be "&", then click "Add" and define a parameter:
- ID: "pet"
- Type: "BOOLEAN"
- The "JWT Authentication" and "Body" sections don't need to be modified
Building the Route Logic in the Flow Editor
The Flow Editor is where we define what happens when our route is called. We'll build a flow that fetches all creatures, loops through them, and returns only the ones that match the pet
URL parameter.
Follow these steps:
-
Add a FETCH block:
- Drag a FETCH block onto the canvas and connect the START node to it
- Fill in the following details:
- Local Name: "_allCreatures"
- Reference Collection: "creatures"
-
Add a PROPERTY block:
- Drag a PROPERTY block onto the canvas and connect the FETCH block to it
- Fill in the following details:
- Local Name: "_allCreaturesLength"
- Property Apply: "LENGTH"
- As for the data section, fill in the following details:
- Reference: Check this box
- Type: "Array"
- Data: "_allCreatures"
-
Add an ASSIGNMENT block:
- Drag an ASSIGNMENT block onto the canvas and connect the PROPERTY block to it
- Fill in the following details:
- Local Name: "_foundCreatures"
- Add an "operation" submodule with 1 operand, and fill in the following details:
- Reference: Leave unchecked
- Type: "Array"
- Data: ""
- Operation Type: "None"
-
Add a LOOP block:
- Drag a LOOP block onto the canvas and connect the ASSIGNMENT block to it
- Fill in the following details:
- Local Name: "index"
- Fill in the following details for the start section:
- Reference: Leave unchecked
- Type: "Integer"
- Data: "0"
- Fill in the following details for the end section:
- Reference: Check this box
- Type: "Integer"
- Data: "_allCreaturesLength"
- Leave everything else as it is
-
Add a PROPERTY block:
- Drag a PROPERTY block onto the canvas and connect the previous LOOP block to it
- Fill in the following details:
- Local Name: "_creature"
- Property Apply: "GET_INDEX"
- Additional: "index"
- As for the data section, fill in the following details:
- Reference: Check this box
- Type: "Array"
- Data: "_allCreatures"
-
Add another PROPERTY block:
- Drag another PROPERTY block onto the canvas and connect the previous PROPERTY block to it
- Fill in the following details:
- Local Name: "_species"
- Property Apply: "GET_PROPERTY"
- Additional: "species"
- As for the data section, fill in the following details:
- Reference: Check this box
- Type: "Other"
- Data: "_creature"
-
Add another PROPERTY block:
- Drag another PROPERTY block onto the canvas and connect the previous PROPERTY block to it
- Fill in the following details:
- Local Name: "_is_pet"
- Property Apply: "GET_PROPERTY"
- Additional: "is_pet"
- As for the data section, fill in the following details:
- Reference: Check this box
- Type: "Other"
- Data: "_creature"
-
Add an ASSIGNMENT block:
- Drag an ASSIGNMENT block onto the canvas and connect the PROPERTY block to it
- Fill in the following details:
- Local Name: "_foundCreatures"
- Add a "condition" submodule with 2 operands, and fill in the following details:
- First Operand:
- Reference: Check this box
- Type: "Boolean"
- Data: "_is_pet"
- Second Operand:
- Reference: Check this box
- Type: "Boolean"
- Data: "pet"
- First Operand:
- Condition Type: "Equal To"
- Add an "operation" submodule with 2 operands, and fill in the following details:
- First Operand:
- Reference: Check this box
- Type: "Array"
- Data: "_foundCreatures"
- Second Operand:
- Reference: Check this box
- Type: "Array"
- Data: "_species"
- First Operand:
- Operation Type: "Addition"
-
Add an END_LOOP block:
- Drag an END_LOOP block onto the canvas and connect the ASSIGNMENT block to it
- Fill in the following details:
- Local Name: "index"
-
Add a RETURN block:
- Drag a RETURN block onto the canvas and connect the END_LOOP block to it
- Add an "object pair" submodule
- Fill in the following details:
- id: "creatures"
- data:
- Reference: Check this box
- Type: "Array"
- Data: "_foundCreatures"
-
Ensure that your Flow Editor looks like the following:
-
Click "Create" to save your route
6. Testing via Playground
Now let's test our creatures API:
- Navigate to Playground in the top navigation bar
- Select your project
- Click on the "FETCH_CREATURES" route
- Enter the value "true" for the "pet" field in the "URL Parameters" section
- Click "Send" to make a request to your API
- You should see a response like:
{ "creatures": ["dog", "cat", "hamster"] }
- Enter the value "false" for the "pet" field in the "URL Parameters" section
- Click "Send" again
- You should see a response like:
{ "creatures": ["scorpion", "kangaroo"] }
Congratulations!
You've successfully built an API that uses a loop to filter data based on user input. You have learned how to:
- Fetch a list of data objects
- Create an empty array to store results
- Loop through a data list
- Use conditional logic within a loop
- Add items to an array dynamically
- Return the filtered results
Next Steps
Challenge yourself by extending this project:
- Add more structures: Add a
diet
(e.g., "carnivore", "herbivore") orhabitat
structure to your collection - More complex filtering: Modify the route to accept multiple filter parameters (e.g.,
pet
anddiet
) - Implement sorting: After filtering, use another loop or a different block to sort the results alphabetically by species
Continuous Improvement
Note: Kinesis API is continuously evolving based on user feedback. As users test and provide suggestions, the platform will become simpler, more intuitive, and easier to use. This tutorial will be updated regularly to reflect improvements in the user experience and new features.
We value your feedback! If you have suggestions for improving this tutorial or the Kinesis API platform, please reach out through our contact page or consider raising a new ticket.
Related Documentation
- Projects - Learn more about managing projects
- Collections - Detailed information about collections
- Structures - Advanced structure options
- Routes - More about creating routes
- Playground - Testing your APIs