Today, we'll examine the four HTTP verbs for CRUD. We will also demonstrate how you can use Retool to create and send API requests to an endpoint, get data from a data source, and test an API’s functionality - all from inside a Retool app.
CRUD is an acronym used to refer to four basic operations that can be performed on database applications: create, read, update, and delete. Many programming languages and protocols have their own equivalent of CRUD. For example, SQL calls the four functions Insert, Select, Update, and Delete. For RESTful APIs, each of the CRUD operations is mapped to an HTTP verb:
|Creates and sends new data to an API endpoint
|Retrieves data from an API endpoint
|Updates existing data
|Removes existing data
Create a new resource for our REST API
Before we can test the four CRUD operations in Retool, we need to create a Resource for the API endpoint we want to play around with.
Login to Retool, and then click on Resources in the top menu bar. Then click on Create new -> Resource -> REST API
Give the API a name, like Books Demo API, and set the Base URL to
Test the CRUD operations inside your Retool app
To play around with the CRUD operations for this resource, we will build a simple Retool app. You can see the app in action here, or you can also download the application JSON definition, and import it into your own Retool instance to play around with it yourself.
You will notice that the resource we created is already selected, with a few pre-defined queries on the left. We are now ready to test our API endpoint using the four HTTP methods.
Sending a GET Request
We will use the GET method to retrieve all books from our Books Demo API. On the bottom panel, select the query getBooks, and then click on Run to test the query.
Sending a POST Request
We will use the POST method to create a new book record. On the bottom panel, select the query addNewBook, and then click on Run to test the query.
You can run the getAllBooks query again to confirm this as well.
Sending a PUT Request
We will use the PUT method to update the book information identified by "id”, and update the price for the book we just added. On the bottom panel, select the query updateBook, and then click on Run to test the query.
Sending a DELETE Request
We will use the DELETE method to delete a book identified by "id", and delete the book we just added. On the bottom panel, select the query deleteBook, and then click on Run to test the query. You should see a success response, along with a JSON response confirming that the record was deleted.
(BONUS) Sending a PATCH Request
While we can use the PUT method to update a record, it requires us to specify all attributes even if we want to change only one attribute. The PATCH method allows us to update only the fields we need, instead of including all fields in our request.
We will use the PATCH request to update the price of a book. On the bottom panel, select the query updateAuthor, and then click on Run to test the query. You should see a success response, along with a JSON response confirming that the record was deleted.
Give it a REST!
Today, we learned how to use a Retool app to create and test basic CRUD operations, using a REST API. We also learned you can quickly create a sample API endpoint using the free Generate API from CSV tool.
I hope you enjoyed playing around with REST APIs today and found this hack useful. Below, you'll find a list of every post from the 12 Days of Retool. Make sure to swing by tomorrow for our next post in the 12 Days of Retool series where we will demonstrate five ways to use the newly launched application sidebar frame.
All 12 Days of Retool posts
- Day 1: A parser for an abstract syntax tree
- Day 2: Two tuple hacks
- Day 3: Three equal signs
- Day 4: Four CRUD operations
- Day 5: Five Sidebar Wins
- Day 6: Six Commands in a Palette
- Day 7: Seven Big Fish Swimming
- Day 8: Eight Bits of Gaming
- Day 9: Nine Star Wars Movies
- Day 10: Ten Retool Videos
- Day 11: Eleven Best FIFA World Cup Games
- Day 12: Twelve Lines of AI Poetry