3.2) Hasura Data API + Postman collection for Expense Tracker

Hasura Expense Tracker
5 min readJun 25, 2017

--

Welcome back readers, hope you have read our blogpost about Data Modelling for Expense Tracker. In this blogpost, we will learn to use Hasura Data API’s for making CRUD (Create, Read, Update and Delete) queries to our Hasura local development project.

How Hasura Data APIs work?

The data APIs by Hasura use the HTTP protocol to communicate to the PostgreSQL database existing in our local project. These APIs can be used by any frontend interfaces such as Android/iOS application, Web application, etc. The API sends a JSON request which is converted into an SQL statement, executed on the PostgreSQL database whose results are returned back to the user in JSON format.

For making these API requests, we will be using Postman app. Make sure you have installed the same.

There are 2 ways by which we can make data requests using Hasura Data APIs to our PostgreSQL database :

  1. Making requests on same machine using Hasura Local Development. In this method, you will get a external endpoint like : http://data.c100.hasura.me/
  2. Exposing Hasura Local Development to the Internet and making requests from same / different machine. In this method, you will get external endpoint like : http://data.<project-name>.hasura-app.io/

For the purpose of the blogpost we will be using method 1, but the same steps can be performed for method 2 as well.

Steps for make database calls using Hasura Data API

  • First log in to your Hasuractl console, click the Data & Schema management icon on the left side and click on Manage. Now note down you External Endpoint. For us, it is http://data.c100.hasura.me/.
External Endpoint for Expense Tracker running on local machine
  • Next, open Postman app and set it up as shown below:
Setup for making data API requests

Select the Body tab and make sure you specify that you wish to make a POST request. The endpoint to which you have to make the request will be of the form : http://external-endpoint/v1/query. Specify that you want to provide raw data and select Content-Type as JSON(application/json).

  • Another important step is to specify your admin token to perform any operations on the database. For getting the admin token, login to Hasuractl console. You will find your admin token specified under Project Info section. Note down your admin token.
Admin Token for Authentication

Under the Headers tab, enter a new key “Authorization and set its value as “Bearer <admin-token>”.

Congratulations, now you are ready to use Hasura Data APIs to make database calls.

1) Inserting Data

Suppose, we wish to insert the categories “Health” and “Movies” in our “Category” table. Let us see the JSON query for the same.

JSON query for Insert operation on Category table
JSON response for Insert query

In the JSON response, we observe that the insert operation has executed successfully, thereby affecting (inserting) 2 rows having “category_id” as 6 and 7 respectively.

2) Querying Data

After inserting data, we may have a need to query the data. Suppose, we wish to view the available categories of expenses which the user can choose from. In this scenario we have to query data from the “Category” table.

JSON query for Select operation on Category table
JSON response for Select query

In the JSON response, we observe that the select operation returns the “category_name” of various categories in the “Category” table.

3) Updating Data

Suppose, we want to update the “exp_name” and “exp_amt” from the “Expense” table.

Currently, for “exp_id” 2 we see the “exp_name” and “exp_amt” as follows :

Current values for “exp_id” 2

Suppose, we want to update the “exp_name” to “Daily Grocery” and “exp_amt” to 1000. Let us see the JSON query for the same.

JSON query for Updating exp_name and exp_amt
JSON response for update operation

From the response, we observe that 1 row has been affected (updated). To verify the values of “exp_name” and “exp_amt” for “exp_id” as 2, we query the “Expense” table as follows :

Querying “Expense” table where “exp_id” is 2

From the response of querying the “Expense” table, we verify that the “exp_name” is set to “Daily Grocery” and “exp_amt” is set to 1000.

4) Deleting Data

Suppose, we wish to delete the “Movies” category from the “Category” table. For this purpose, we may use the following JSON query :

JSON query for Delete operation on Category table
JSON response for Delete query

In the JSON response, we observe that 1 row has been affected (deleted) from the “Category” table.

To confirm that the “Movies” category has been deleted, we can perform the “Select” operation as follows :

Querying all rows in Category table

We observe that the “Movie” category has been successfully deleted from the “Category” table.

The Data API collection used is as follows : https://www.getpostman.com/collections/0b62d1fd5a9daa3980f5

There are many more services provided by the Hasura APIs which will be discussed in the next blogpost.

References :

  1. Documentation for Hasura Data APIs
  2. Examples of Hasura Data APIs

-Vinit Neogi and Akash Dabhi

--

--

Hasura Expense Tracker

Two tech geeks embarking on a journey to create their first android app for their Hasura Internship program. IT dept, St. Francis Institute of Technology.