Design a serverless REST API – IBM cloud functions and cloudant
In this tutorial, we’ll create a serverless web application using IBM Cloud™ Functions and Cloudant. Our REST API will serve as a simple to-do list, allowing users to view, add, edit, and delete tasks.
Objectives
- Deploy a serverless backend and a database
- Create actions for different tasks:
- Add a to-do
- View to-dos
- Edit a to-do
- Delete a to-do
- Expose a REST API for all actions
If you prefer a video tutorial, you can watch this video or continue reading.
You can follow their official tutorial on creating a REST API. This tutorial is a good starting point if you are just trying out.
Prerequisites
You need an IBM Cloud account. If you don’t have one, you can register here. The Lite account is free and does not require a credit card.
Step 1: Create a Database
- Log in to IBM Cloud and go to the Catalog.
- Search for Cloudant under Databases and select it.
- Configure the Cloudant instance:
- Choose a region (e.g., Chennai or one nearest to you).
- Set a unique name (e.g.,
Cloudant-todolist). - Choose the Lite plan.
- Click Create and wait for the instance to become active.
- Navigate to Service Credentials on the left menu and click New Credential:
- Name:
for-todolist - Role: Manager
- Click Add
- Name:
- Click Launch Dashboard to access Cloudant.
- Click Create Database:
- Name:
todolist - Partitioning: Non-Partitioned
- Name:
Step 2: Create Serverless Actions
We will create serverless actions to manage the to-do list.
Add To-Do Action
- Go to Functions > Actions > Create Action.
- Set the name as
prepare-entry-for-save. - Create a package named
todolist. - Select Node.js as the runtime.
- Replace the default code with:
function main(params) {
if (!params.name) {
return Promise.reject({ error: "No name provided" });
}
return {
doc: {
createdAt: new Date(),
name: params.name,
completed: false,
},
};
}
- Click Save.
- Click Enclosing Sequences > Add to Sequence.
- Set sequence name as
save-todo-to-sequenceand choosetodolistas the package. - Click Create and Add.
Retrieve To-Dos Action
- Create a new action named
set-read-inputunder thetodolistpackage. - Replace the default code with:
function main(params) {
return { params: { include_docs: true } };
}
- Click Save.
- Follow similar steps to add a sequence named
read-todolist-entries-sequence. - Add Cloudant’s
list-documentsaction using the previously createdbinding-for-todolist. - Create a new action
format-entries, replace the default code with:
function main(params) {
return {
entries: params.rows.map((row) => ({
name: row.doc.name,
completed: row.doc.completed,
_id: row.doc._id,
_rev: row.doc._rev,
createdAt: row.doc.createdAt,
})),
};
}
- Click Save.
Edit To-Do Action
- Create a new action
update-todo-itemundertodolist. - Replace the code with:
function main(params) {
if (!params.doc) {
return Promise.reject({ error: "No document provided" });
}
return { doc: { ...params.doc, createdAt: new Date() } };
}
- Click Save.
- Create a sequence
update-todo-entry-sequenceand link Cloudant’supdate-documentsaction.
Delete To-Do Action
- Create an action
delete-todo-itemundertodolist. - Replace the code with:
function main(params) {
if (!params.docid || !params.docrev) {
return Promise.reject({ error: "docid and docrev are required" });
}
return { docid: params.docid, docrev: params.docrev };
}
- Click Save.
- Create a sequence
delete-todo-entry-from-sequenceand link Cloudant’sdelete-documentsaction.
Step 3: Create an API
- Enable all four action sequences as Web Actions.
- Go to APIs > Create API.
- Set API name to
todolistand base path to/api. - Create operations for each sequence:
- GET /entries ->
read-todolist-entries-sequence - PUT /entries ->
save-todo-to-sequence - PATCH /entries ->
update-todo-entry-sequence - DELETE /entries ->
delete-todo-entry-from-sequence
- GET /entries ->
- Click Create API and note the API route.
My Thoughts on IBM Cloud
As a self-taught web developer, I rely heavily on documentation. IBM Cloud lacks detailed documentation, making it harder for beginners. While complexity isn’t an issue for developers, clear guides, code snippets, and small tutorials can significantly improve the experience.
Conclusion
We’ve successfully created a serverless REST API using IBM Cloud Functions and Cloudant. This setup provides a scalable, cost-effective solution for a to-do list application. Try extending this by adding authentication or a frontend!