3.2 Getting started with Hasura data APIs

Hasura.io

In this post I will breeze you through the Hasura data APIs with examples to use it on my Engineering Assistant app database.

In Computer Programming, CRUD stands for Create, Read, Update, Delete and these are the four basic functions of data storage.

Data API :

http://data.c100.hasura.me/

This can be found in Data>Manage>Access- External endpoint part of the console.

Before you connect your webapp’s frontend with the backend , you can send API requests using a very useful tool called POSTMAN and check if you are getting the right response for your requests.

Endpoint:

POST : http://data.c100.hasura.me/v1/query

Create

Inserting data into tables.

Inserting Topic names into the topic table

-H "Authorization: auth_code"
-H "Content-Type: application/json"
-d {
"type":"insert",
"args": {
"table":"topic",
"objects":[
{
"topic_name":"Android"
},
{
"topic_name":"Algorithms"
}
]
}
}

Inserting a resource into the resource table by a logged in user

-H "Authorization: auth_code"
-H "Content-Type: application/json"
-d {
"type":"insert",
"args":{
"table":"resource",
"objects":[
{
"resource_url":"https://developer.google.com",
"name":"Google Android",
"topic":"Android",
"user_id":4,
"type":"Guide",
"certificate":false,
"cost":false
}
]
}
}

Read

Retrieving data form the tables.

Get all the information related to a resource form the resource_info view and order it in descending order of total votes.

-H "Content-Type: application/json"
-d {
"type":"select",
"args": {
"table": "resource_info",
"columns": ["*"],
"order_by": {
"column" : "total_votes",
"order" : "desc"
"null" : "last"
}
}
}
         }
}
}

Update

Modify data of a table.

Update a resource url in case it has been moved or if it is a wrong link.

-H "Authorization: auth_code"
-H "Content-Type: application/json"
-d { "type" : "update",
"args" : {
"table":"resource",
"$set":{ "resource_url":"https//www.developers.google.com" },
"where":{ "resource_url":"https://developer.google.com" }
}
}

Delete

Remove a row from a table.

Remove a user’s vote form the vote table if he changes his mind.

-H "Authorization: auth_code"
-H "Content-Type: application/json"
-d { "type" : "delete",
"args" : {
"table" : "votes",
"where" : {
"user_id" :3 ,
"resource":"https//www.developer.google.com"
}

}
}

Have a look at this Postman Collection to see all the requests.

Next