This guide will help you set up all the required services and connect them properly.
- Go to Nhost Console
- Create a new project
- Note down your:
- Subdomain (e.g.,
abcdef123456) - Region (e.g.,
us-east-1) - Hasura endpoint will be:
https://abcdef123456.hasura.us-east-1.nhost.run/v1/graphql
- Subdomain (e.g.,
- In Nhost console → Settings → Authentication
- Enable "Email + Password" sign-in method
- Set allowed redirect URLs to include your domain
- From Nhost dashboard, click "Open Hasura"
- Or go directly to:
https://abcdef123456.hasura.us-east-1.nhost.run/console
- Important: Make sure you're accessing Hasura Console with admin permissions
- Go to Data → SQL
- UNCHECK the "Read only" checkbox at the bottom of the SQL editor
- CHECK the "Track this" checkbox to track the new tables automatically
- Run the SQL from
hasura-setup.sql:- Open the
hasura-setup.sqlfile in your project folder - Copy the entire SQL content from the file
- In Hasura Console, paste the SQL into the SQL editor text area
- Make sure "Read only" is UNCHECKED and "Track this" is CHECKED
- Click "Run!" button to execute all the SQL commands
- You should see success messages for each table creation and policy setup
- Open the
- This creates
chatsandmessagestables with proper RLS
If you get the error "cannot execute CREATE TABLE in a read-only transaction":
Option A: Uncheck "Read only" Checkbox
- Make sure you UNCHECK the "Read only" checkbox at the bottom of the SQL editor
- The "Read only" checkbox prevents any write operations to the database
- When unchecked, you can create tables and modify the database schema
Option B: Use Raw SQL Tab (Alternative)
- Instead of Data → SQL, go to Data tab
- Look for a "Raw SQL" or "Execute SQL" option
- Some Hasura versions have this in a different location
Option C: Verify Admin Access
- Ensure you're accessing Hasura Console with admin privileges
- If using Nhost, make sure you clicked "Open Hasura" from the Nhost dashboard
- Check that your Hasura admin secret is correct
Option D: Execute Commands Step by Step If the bulk execution fails, use the separate SQL files:
- Run
hasura-step1-tables.sql- Creates the tables - Run
hasura-step2-rls.sql- Creates indexes and enables RLS - Run
hasura-step3-policies.sql- Creates security policies (Each file contains smaller, focused SQL commands that are less likely to fail)
Step 2.1: Access the SQL Editor
- In Hasura Console, click on the "Data" tab in the top navigation
- Click on "SQL" in the left sidebar (below the database tables list)
- You'll see a SQL editor with a text area and a "Run!" button
Step 2.2: Copy the SQL Content
- Open the
hasura-setup.sqlfile from your project root directory - Select all content (Ctrl+A) and copy (Ctrl+C)
- The file contains:
-- Create chats table CREATE TABLE public.chats ( id uuid DEFAULT gen_random_uuid() NOT NULL, title text NOT NULL, user_id uuid NOT NULL, created_at timestamp with time zone DEFAULT now() NOT NULL, updated_at timestamp with time zone DEFAULT now() NOT NULL, PRIMARY KEY (id), FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE ); -- ... and more SQL commands
Step 2.3: Configure Checkboxes and Execute
- Paste the copied SQL into the Hasura SQL editor text area
- IMPORTANT: Look at the checkboxes at the bottom:
- UNCHECK "Read only" - This allows write operations
- CHECK "Track this" - This automatically tracks new tables in Hasura
- "Cascade metadata" can remain as is (usually checked by default)
- Review the SQL to ensure it's complete (should include CREATE TABLE, ALTER TABLE, CREATE POLICY statements)
- Click the "Run!" button (usually orange/blue colored)
- Wait for execution to complete
Step 2.4: Verify Success
- You should see output messages like:
✅ CREATE TABLE ✅ CREATE INDEX ✅ ALTER TABLE ✅ CREATE POLICY - If you see any errors, check the SQL syntax and try again
- After successful execution, you'll see
chatsandmessagestables appear in the left sidebar under "public" schema
- Go to Data → chats → Permissions
- Add permissions for
userrole as described inhasura-permissions.md - Repeat for
messagestable
- In
chatstable → Relationships:- Array relationship:
messages(chats.id → messages.chat_id)
- Array relationship:
- In
messagestable → Relationships:- Object relationship:
chat(messages.chat_id → chats.id)
- Object relationship:
- Go to Actions → Create Action
- Follow the configuration in
hasura-action-config.md - Set webhook URL to your n8n webhook (you'll get this in step 4)
- Go to OpenRouter
- Sign up/Sign in
- Go to API Keys section
- Create a new API key
- Note: They offer free models like
meta-llama/llama-3.1-8b-instruct:free
- Go to n8n Cloud
- Create account and workspace
- Install n8n:
npm install n8n -g - Run:
n8n start - Access at
http://localhost:5678
- In n8n, go to Workflows → Import from File
- Import
n8n-workflow.json - Set environment variables in n8n:
HASURA_ENDPOINT: Your Hasura GraphQL endpointHASURA_ADMIN_SECRET: Your Hasura admin secretOPENROUTER_API_KEY: Your OpenRouter API key
- In the workflow, click on "Webhook Trigger" node
- Copy the webhook URL (e.g.,
https://your-n8n.app.n8n.cloud/webhook/chatbot) - Use this URL in your Hasura Action configuration
- Copy
.env.exampleto.env - Fill in the actual values from your services:
VITE_NHOST_SUBDOMAIN=your-actual-nhost-subdomain
VITE_NHOST_REGION=us-east-1
VITE_HASURA_GRAPHQL_ENDPOINT=https://your-hasura-endpoint.hasura.app/v1/graphql
VITE_HASURA_WS_ENDPOINT=wss://your-hasura-endpoint.hasura.app/v1/graphqlnpm installnpm run devnpm run build- Build the project:
npm run build - The
dist/folder contains your built app
- Go to Netlify
- Drag and drop your
dist/folder, OR - Connect your Git repository for continuous deployment
- Go to Site settings → Environment variables
- Add all your
VITE_*environment variables - Redeploy the site
- Create
public/_redirectsfile with content:
/* /index.html 200
- This ensures React Router works properly on Netlify
- Sign up with a new email
- Verify you're redirected to the chat interface
- Create a new chat
- Verify it appears in the chat list
- Send a message in a chat
- Verify the message appears immediately
- Verify the bot response arrives within a few seconds
- Check that the response appears in real-time
- Check that users can only see their own chats
- Test that the n8n workflow validates chat ownership
- Verify that direct API calls to OpenRouter are blocked
- Check Hasura permissions for
messagestable - Verify WebSocket connection in browser dev tools
- Check n8n workflow execution logs
- Verify Hasura Action is configured correctly
- Check n8n webhook is receiving requests
- Verify OpenRouter API key is working
- Check n8n execution logs
- Verify Nhost configuration
- Check JWT token in localStorage
- Verify Hasura receives proper headers
- Ensure all environment variables are set
- Check that
_redirectsfile is in place for Netlify - Verify build completes without errors
- All environment variables configured
- Database tables created with RLS enabled
- Hasura permissions set up correctly
- n8n workflow imported and tested
- OpenRouter API key working
- Authentication flow tested
- Real-time subscriptions working
- Chat ownership validation working
- App deployed to Netlify
- Custom domain configured (optional)
- SSL certificate active
- Never expose your Hasura admin secret to the frontend
- Always validate chat ownership in n8n before processing
- Use HTTPS for all external API calls
- Keep OpenRouter API key secure in n8n environment
- Implement rate limiting if needed
- Monitor API usage and costs
This setup ensures a secure, scalable chatbot application that meets all the assignment requirements!