Connect to Hasura Programmatically

Up until now, you were taking advantage of YugabyteDB and Hasura’s Consoles to create, read, and update the data. In this final bonus section, you’ll learn how to connect to a Hasura public endpoint from your laptop and query records programmatically.

Query via Curl Tool

The curl command-line tool is one of the most straightforward and universal ways to check the connectivity between your laptop and the Hasura GraphQL endpoint.

Query the Todos table by sending a POST request to the Hasura endpoint:

  1. Locate the GraphQL API public IP address and Admin Secret in the settings of your Hasura project:

  1. Provide your API address and secret in the curl command below by replacing the GRAPHQL_API_ENDPOINT and GRAPHQL_ADMIN_SECRET placeholders:
curl -X POST \
-H "Content-Type: application/json" \
-H "X-Hasura-Admin-Secret: GRAPHQL_ADMIN_SECRET" \
-d '{"query":"query {todos {title user {id name}}}"}' \
GRAPHQL_API_ENDPOINT
  1. Execute the curl request and confirm the output is as follows:
{
"data": {
"todos": [
{
"title": "Complete Hasura & YugabyteDB Tutorial",
"user": {
"id": 1,
"name": "Mark"
}
}
]
}
}

Query via Node.js Application

Callout: To proceed with this section you need to download and install npm and Node.js.

Finally, download and execute a sample Node.js application that connects to your Hasura BaaS and retrieves a list of Todos:

  1. Clone the project with the sample application:
git clone https://github.com/yugabyte/examples.git && cd examples/yugabyte-hasura-snippets/
  1. Add the minimal GraphQL client for Node to the project:
npm add graphql-request graphql
  1. Open the examples/yugabyte-hasura-snippets/app.js file.

  2. Provide your GrahpQL API address and Admin Secret in the app.js source code by replacing the GRAPHQL_API_ENDPOINT and GRAPHQL_ADMIN_SECRET placeholders.

  3. Launch the application:

node app.js
  1. Confirm it produces output as follows:
{
"todos": [
{
"title": "Complete Hasura & YugabyteDB Tutorial",
"user": {
"id": 1,
"name": "Mark"
}
}
]
}
Did you find this page helpful?
Start with GraphQL on Hasura for Free
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2024 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin