Spaces:
Sleeping
π Frontend Connection to Hugging Face Backend
β Configuration Complete!
Your Next.js frontend is now configured to connect to your Hugging Face Space backend.
π Changes Made
1. β Frontend Environment Configuration
Created: frontend/.env.local
NEXT_PUBLIC_API_URL=https://baveshraam-open-notebook.hf.space
INTERNAL_API_URL=https://baveshraam-open-notebook.hf.space
This tells the frontend where to find your deployed backend API.
2. β API Client Already Configured
File: frontend/src/lib/api/client.ts
The API client already uses dynamic configuration:
- β
Reads from
process.env.NEXT_PUBLIC_API_URL - β Falls back to auto-detection
- β
Ultimate fallback to
http://127.0.0.1:5055
No changes needed! The existing code will automatically use your .env.local settings.
3. β CORS Configuration Updated
File: api/main.py
Updated CORS to allow requests from:
- β
http://localhost:3000(local development) - β
http://127.0.0.1:3000(local development) - β
https://baveshraam-open-notebook.hf.space(your HF Space) - β
*(wildcard - allows any origin)
4. β Hardcoded URLs Checked
All hardcoded localhost:5055 references in the frontend are:
- β Fallback defaults only (when env vars not set)
- β Example text in error messages (documentation)
- β No action needed - proper fallback behavior
π― Next Steps
Step 1: Deploy Backend Changes to Hugging Face
The CORS update needs to be deployed to your Hugging Face Space:
git add api/main.py
git commit -m "Update CORS for frontend connection"
git push
Wait for Hugging Face to rebuild (check the Space logs).
Step 2: Start Your Frontend Locally
cd frontend
npm install # If not already done
npm run dev
Your frontend will start on http://localhost:3000 and connect to:
- Backend API:
https://baveshraam-open-notebook.hf.space
Step 3: Test the Connection
- Open browser: http://localhost:3000
- Check browser console for API connection messages
- Try creating a notebook or any API-dependent feature
- Check Network tab to verify requests go to your HF Space URL
π How It Works
Configuration Priority
The frontend uses a smart fallback system:
1. Runtime config from /config endpoint (uses .env.local)
β
2. Build-time NEXT_PUBLIC_API_URL
β
3. Auto-detection from browser URL
β
4. Fallback to http://127.0.0.1:5055
Environment Variables
| Variable | Used By | Purpose |
|---|---|---|
NEXT_PUBLIC_API_URL |
Browser | Client-side API calls |
INTERNAL_API_URL |
Next.js Server | Server-side proxying |
URL Structure
The frontend automatically constructs API URLs:
- Base URL:
https://baveshraam-open-notebook.hf.space - API Endpoint:
/api(added automatically) - Full API URL:
https://baveshraam-open-notebook.hf.space/api
π οΈ Troubleshooting
Issue: "Failed to fetch" or CORS errors
Solution:
- Verify backend is running: https://baveshraam-open-notebook.hf.space/health
- Check backend logs for CORS rejections
- Ensure CORS changes were deployed (check git commit)
Issue: Frontend still connects to localhost
Solution:
- Verify
.env.localfile exists infrontend/directory - Restart Next.js dev server:
npm run dev - Check browser console for config messages
- Clear browser cache and reload
Issue: 404 errors on /api/* endpoints
Solution:
- Check that backend is running: https://baveshraam-open-notebook.hf.space/api/config
- Verify the URL doesn't have double
/api/api/ - Check Next.js rewrite configuration in
next.config.ts
π Environment Files Reference
.env.local (active configuration)
Your current deployment settings.
.env.local.example (template)
Copy this when deploying to new environments.
.env.example (backend configuration)
Backend environment variables (already configured on HF Space).
π Success Indicators
You'll know the connection works when:
- β
Browser console shows:
β [Config] Successfully loaded API config - β
Network tab shows requests to
baveshraam-open-notebook.hf.space - β No CORS errors in browser console
- β Features like "Create Notebook" work correctly
- β Health check responds: https://baveshraam-open-notebook.hf.space/health
π Deploy Frontend (Optional)
When ready to deploy your frontend:
Vercel / Netlify
Add environment variables in dashboard:
NEXT_PUBLIC_API_URL=https://baveshraam-open-notebook.hf.space
INTERNAL_API_URL=https://baveshraam-open-notebook.hf.space
Docker
docker build -t open-notebook-frontend ./frontend
docker run -p 3000:3000 \
-e NEXT_PUBLIC_API_URL=https://baveshraam-open-notebook.hf.space \
-e INTERNAL_API_URL=https://baveshraam-open-notebook.hf.space \
open-notebook-frontend
π Architecture Overview
βββββββββββββββββββββββ
β Browser β
β localhost:3000 β
ββββββββββββ¬βββββββββββ
β NEXT_PUBLIC_API_URL
β
βββββββββββββββββββββββββββββββββββββββ
β Hugging Face Space Backend β
β baveshraam-open-notebook.hf.space β
β β
β ββββββββββββ ββββββββββββ β
β β FastAPI β ββββ β SurrealDBβ β
β ββββββββββββ ββββββββββββ β
βββββββββββββββββββββββββββββββββββββββ
π You're All Set!
Your frontend is now ready to connect to your Hugging Face deployed backend. Start the frontend with npm run dev and test away!