Getting started overview
Hasura simplifies the process of building GraphQL APIs by providing an instant API layer over your databases. This guide focuses on the streamlined path to getting a Hasura project operational, connecting a database, and executing your initial GraphQL query. The core product for rapid development is Hasura Cloud, which offers a managed service environment.
The typical getting started workflow involves these steps:
- Account Creation: Sign up for a Hasura Cloud account.
- Project Setup: Create a new Hasura Cloud project.
- Database Connection: Link an existing PostgreSQL database or create a new one using a provided integration.
- Credentials Retrieval: Obtain your GraphQL API endpoint and Admin Secret.
- First Query: Execute a GraphQL query against your new API.
This process enables developers to immediately access data via GraphQL, including real-time subscriptions, without writing boilerplate API code. Hasura's console provides a graphical interface for managing your database schema and exploring your GraphQL API, as detailed in the Hasura Console documentation.
Create an account and get keys
To begin with Hasura, you need to create a Hasura Cloud account and set up your first project. This will provide you with the necessary endpoint and API keys.
Account Creation
- Navigate to the Hasura signup page.
- Sign in using your GitHub account or email address.
- Follow the prompts to complete your account setup.
Project Setup
After creating your account, you will be guided to create a new project:
- On the Hasura Cloud dashboard, click New Project.
- Choose a project name and select a region for your project deployment.
- Hasura will provision your project. This process typically takes a few moments.
Database Connection
Once your project is provisioned, the next critical step is to connect a database. Hasura natively integrates with PostgreSQL databases.
- From your newly created project's dashboard, click Connect Database.
- You have two primary options for database connection:
- Connect your existing database: Provide the connection string for your PostgreSQL database. This is common for existing applications.
- Create a new database: Hasura offers integrations with database providers like Neon, ElephantSQL, or DigitalOcean Managed Databases. Selecting one of these options will guide you through creating a new PostgreSQL database and automatically connect it to your Hasura project. For example, the Hasura Neon integration streamlines this process.
- Ensure the connection string is correctly formatted and accessible by Hasura Cloud.
- Once connected, Hasura will introspect your database schema and instantly generate a GraphQL API.
Retrieve API Endpoint and Admin Secret
Your Hasura project will expose a GraphQL API endpoint and require an Admin Secret for authenticated requests.
- From your project's dashboard on Hasura Cloud, locate the Endpoint field. This is your GraphQL API URL (e.g.,
https://<project-id>.hasura.app/v1/graphql). - Navigate to the Settings tab for your project.
- Under the GraphQL API section, find the Admin Secret. This secret is a secure token required to make authenticated requests to your Hasura API. Copy this value. Keep this secret secure, as it grants administrative access to your GraphQL API.
Your first request
With your Hasura project running, a database connected, and credentials in hand, you can now execute your first GraphQL query. We will use the built-in Hasura Console to explore the API and then demonstrate a request using a common HTTP client.
Using the Hasura Console (GraphiQL)
The Hasura Console includes a GraphiQL interface, an in-browser IDE for GraphQL, which is ideal for testing queries and mutations.
- From your Hasura Cloud project dashboard, click Launch Console.
- In the console, navigate to the API tab.
- The left pane allows you to write your GraphQL query. For example, if you have a table named
authorswith columnsidandname, you could write a query: - Click the Play button (▶) to execute the query. The results will appear in the right pane.
query MyAuthors {
authors {
id
name
}
}
Making a request with curl
To make a request programmatically, you'll need your GraphQL API endpoint and Admin Secret. The following example uses curl to demonstrate a basic query.
Prerequisites:
- Your Hasura GraphQL API endpoint (e.g.,
https://your-project-id.hasura.app/v1/graphql) - Your Admin Secret
- A table in your connected database (e.g.,
userswith anamecolumn)
Steps:
- Open your terminal or command prompt.
- Construct your GraphQL query as a JSON payload. For instance, to fetch all users:
- Execute the
curlcommand, replacing placeholders with your actual endpoint and secret: - Press Enter. The server's GraphQL response, containing the queried data, will be printed to your terminal.
{
"query": "query { users { id name } }"
}
curl -X POST \
-H "Content-Type: application/json" \
-H "x-hasura-admin-secret: YOUR_ADMIN_SECRET" \
"YOUR_HASURA_ENDPOINT" \
-d '{"query": "query { users { id name } }"}'
Here's a breakdown of the curl command options:
-X POST: Specifies the HTTP method as POST. GraphQL requests are typically POST requests.-H "Content-Type: application/json": Sets the content type of the request body.-H "x-hasura-admin-secret: YOUR_ADMIN_SECRET": Provides the authentication token. This is crucial for securing your API. For production, consider using a more robust webhook-based authentication system or JWT integration."YOUR_HASURA_ENDPOINT": The URL of your Hasura GraphQL API.-d '{"query": "query { users { id name } }"}': The request body containing the GraphQL query.
This successful response confirms that your Hasura project is correctly configured and accessible.
Common next steps
After successfully making your first request, consider these common next steps to further develop your application with Hasura:
- Data Modeling: Use the Hasura Console to create and manage tables, columns, and relationships in your database. Hasura automatically updates the GraphQL schema.
- Permissions: Implement granular access control. Hasura allows you to define row-level and column-level permissions based on user roles, as outlined in the Hasura permissions documentation.
- Relationships: Define relationships between your tables (e.g., one-to-many, many-to-many) in the Hasura Console. This enables complex nested queries in GraphQL.
- Actions & Remote Schemas: Extend your GraphQL API with custom business logic using Actions (for custom mutations and queries) or integrate existing GraphQL services using Remote Schemas.
- Event Triggers: Automate workflows by setting up Event Triggers to react to database changes (inserts, updates, deletes) and invoke webhooks.
- API Clients: Integrate your Hasura API with front-end or back-end applications using GraphQL client libraries. Popular choices include Apollo Client for JavaScript frameworks or URQL, both of which are common for building GraphQL applications with React.
- Subscriptions for Realtime: Explore GraphQL Subscriptions to enable real-time data updates in your applications. The Hasura Subscriptions guide provides implementation details.
Troubleshooting the first call
Encountering issues during your first API call is common. Here are some troubleshooting steps:
- Incorrect Endpoint: Double-check that the GraphQL API endpoint used in your request exactly matches the one displayed in your Hasura Cloud project dashboard. Verify there are no typos, extra slashes, or incorrect protocols (
httpvs.https). - Admin Secret Mismatch: Ensure the
x-hasura-admin-secretheader value is correct and hasn't been truncated or altered. Copy it directly from the Hasura Cloud console. - Network Issues: Check your internet connection. If you are behind a corporate firewall or VPN, ensure that traffic to Hasura Cloud (port 443) is not blocked.
- Database Connectivity: Verify that your database is running and accessible from Hasura Cloud. Check the Hasura Console's Data tab for any database connection errors. Ensure your database allows connections from Hasura's IP ranges, especially if it's shielded by a firewall.
- GraphQL Query Syntax: Even minor syntax errors in your GraphQL query can cause issues. Use the Hasura Console's GraphiQL interface to validate your query syntax before attempting it with
curlor other clients. GraphiQL provides real-time validation and suggestions. Refer to the official GraphQL queries documentation for syntax rules. - Missing Tables/Columns: If your query refers to tables or columns that do not exist or have not been tracked by Hasura, the API will return an error. Use the Hasura Console's Data tab to confirm your tables are tracked and that the schema matches your query.
- Content-Type Header: The
Content-Typeheader must be set toapplication/jsonfor GraphQL POST requests. - Server Logs: Check the Hasura Cloud project logs for error messages. These logs can provide specific details about why a request failed, such as authentication errors, database connection issues, or GraphQL parsing errors.
| Step | What to Do | Where |
|---|---|---|
| 1. Sign Up | Create a Hasura Cloud account. | Hasura Signup Page |
| 2. Create Project | Provision a new Hasura Cloud project. | Hasura Cloud Dashboard |
| 3. Connect Database | Link an existing PostgreSQL DB or create a new one. | Project Dashboard > Connect Database |
| 4. Get Endpoint & Secret | Retrieve GraphQL API URL and Admin Secret. | Project Dashboard > Settings > GraphQL API |
| 5. Test Query (Console) | Execute a sample query using GraphiQL. | Project Console > API tab |
| 6. Test Query (Programmatic) | Use curl or an SDK to make a request. |
Your terminal / code editor |