Skip to main content

CRUD APIs for a TODO App

Learn how to interact with the fastn database.

Introduction

  • In this guide, we are going to build CRUD (create, read, update and delete) APIs for a TODO list that are going interact with the fastn internal database to manage tasks.

Part I - Create a Task

Building a createTask flow

  • To start off, go to the "Flows" page from the navigation menu on the left. Add a new Flow.

flow-page

  • Name your flow "createTask" and create it as an API.

flow-creation-menu

  • In the "Flow Configuration" step, add a new model.

model-1

  • Set the new model "newTask" as the input.

model-1

  • Defining a model will setup the input schema for the createTask API. This API will take in a task title and create a new task with that title.

Create a task in the database

  • Drag and drop a database element and name it "CreateTask".

  • Set it up with the following query.

CREATE TABLE IF NOT EXISTS tasks (
id varchar(255) primary key,
title varchar(255) not null
);

INSERT INTO tasks VALUES (gen_random_uuid(), '{{input.title}}');

db-1

  • This query will create a table for tasks with each task having the fields id and title as attributes.

  • It will then insert a new task with the title passed in the input and a random uuid as the id.

  • Add a "Success" response and have it return a message like "New Task Created".

  • We can test the create task flow using the "Debugger".

flow-1

  • This will create a new task in the database. To see the tasks, navigate to the "Databases" page and select fastn DB.

Fastn-database

  • Select the "tasks" table.

db-1

  • Here you can see all the current tasks present in the table and filter data as needed.

db-1

Part II - Read Tasks

Building a getTask API

  • Create a new API flow and name it "getTask".

  • Go to the Flow Configuration step and add a new model. Name it "taskById". Set it as the input model.

model-1

  • Add a database step with the following query.
SELECT * FROM tasks WHERE id = '{{input.id}}';

db-1

  • We can test this flow in the "Debugger".

flow-1

Building a getTasks API

  • Create a new API flow and name it "getTasks".

  • We don't need a specific model here. So we can add the database step with the following query.

SELECT * FROM tasks

db-1

  • We can test this flow in the "Debugger".

flow-1

Part III - Update a Task

Building an updateTask API

  • Create a new API flow and name it "updateTask".

  • Go to the "Flow Configuration" step and add a new model. Name it "updateTask". Set it as the input model.

model-1

  • Add a database step with the following query.
UPDATE tasks
SET description = '{{input.title}}'
WHERE id = '{{input.id}}';

db-1

  • Add a "Success" response with an appropriate message.

  • We can test this flow in the "Debugger".

flow-1

Part IV - Delete a Task

Building a deleteTask API

  • Create a new API flow and name it "deleteTask".

  • Go to the "Flow Configuration" step and set the "taskById" model we created before as the input model.

model-1

  • Add a database step with the following query.
DELETE FROM tasks WHERE id = '{{input.id}}';

db-1

  • Add a "Success" response with an appropriate message.

  • We can test this flow in the "Debugger".

flow-1

Part V - Integrating APIs in your App

  • After completing the setup in Parts I to IV, we should have the following flows.

flow-1

  • To integrate a flow as an API. Go to a flow and click on "Deploy".

  • Once a flow is deployed, click on "Embed this Flow".

  • Use "Generate API Key" to create new API key with access to desired flows. Note: If you have already generated an API key with permissions for multiple flows it can be reused in the "x-fastn-api-key" header.

embed-1

  • Once the API key is generated the auto-generated code will be updated. Select the desired programming language or copy the cURL request and integrate in your app or use it with an API tool like Postman.

embed-2

Mission Success

Congratulations! Now you know how to integrate APIs in an application and interact with the fastn database