Getting badges

Now that we have some badges in the database, we need to be able to get them out of there. This part of the tutorial series explains how to retrieve the badges from the database.

Step 1: Create an "API" flow

  1. In the Explorer pane, click Create new.
  2. Select API.
  3. Type Get badges.
  4. Tap the Return key.

Step 2: Configure the "Start" component

  1. Open the Start component.
  2. From the Method dropdown, select GET.
  3. In the URL field, enter /get-badges.

Step 3: Get the badges from the database

  1. Add a Select component to the flow.
  2. Rename the component to getBadges.
  3. From the Table dropdown, select badges.

Step 4: Check if badges exist

Most components have two connection points: an input and an output.

Some components though, such as the If component, have three connection points: one input and two outputs. The extra output adds branching logic to the flow. This lets the flow progress in different directions when certain conditions are met.

In the Get badges flow, we can use an If component to check if any badges were found in the database. If badges aren't found, we can respond to the request with an error. Otherwise, we can respond with the badges.

To set up the If component:

  1. Add an If component to the flow.
  2. Create a condition that checks if getBadges.data is not null or empty.

Step 5: If badges don't exist, return an error

  1. Add an End component to the flow.
  2. Connect the If component to the End component via the False branch.
  3. Set the Status Code to 404 Not Found.

Step 6: If badges do exist, return the badges

  1. Add an End component to the flow.
  2. Connect the If component to the End component via the True branch.
  3. From the Status Code dropdown, select 200 OK.
  4. From the Content Type dropdown, select application/json.
  5. In the Body field, enter getBadges.data.

This is what the complete flow looks like: