Public
Likenanosupabase-demo
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Viewing readonly version of main branch: v1View latest version
Deploy nano-supabase as a serverless API on Val.town.
- Go to val.town and create a new HTTP val
- Copy the contents of
index.tsinto the editor - Your API is instantly live at
https://YOUR_USERNAME-YOUR_VAL_NAME.web.val.run
# Install vt CLI npm install -g @valtown/vt # Login vt login # Create the val from this folder cd examples/valtown vt create my-chat-api . --no-editor-files --upload-if-exists --public # Or clone and modify an existing one vt clone filipecabaco/nano-chat
| Method | Path | Description |
|---|---|---|
| GET | / | API info and available endpoints |
| GET | /conversations | List all conversations |
| POST | /conversations | Create conversation { title: string } |
| GET | /messages?conversation_id=xxx | Get messages for a conversation |
| POST | /messages | Add message { conversation_id, role, content, tokens? } |
| GET | /stats | Get usage statistics |
# Get API info curl https://YOUR_USERNAME-YOUR_VAL_NAME.web.val.run/ # Create a conversation curl -X POST https://YOUR_USERNAME-YOUR_VAL_NAME.web.val.run/conversations \ -H "Content-Type: application/json" \ -d '{"title": "Test Chat"}' # Add a message curl -X POST https://YOUR_USERNAME-YOUR_VAL_NAME.web.val.run/messages \ -H "Content-Type: application/json" \ -d '{"conversation_id": "YOUR_ID", "role": "user", "content": "Hello!"}'
Val.town workers are stateless - the database resets on cold starts. For persistence, use Val.town's blob storage:
import { blob } from "https://esm.town/v/std/blob";
// Save state periodically
async function saveState() {
const dump = await db.dumpDataDir();
await blob.setJSON("chat-db-state", dump);
}
// Restore on startup (in getDb function)
const saved = await blob.getJSON("chat-db-state");
if (saved) {
db = new PGlite();
await db.loadDataDir(saved);
}
The example uses a chat schema, but you can modify index.ts to:
- Add your own tables and schemas
- Implement different API endpoints
- Add authentication
- Connect to AI APIs for chat completions
"Invalid version provided" error: This usually means there's a caching issue. Try:
- Delete the val and create a new one with a different name
- Clear Val.town's module cache by changing the import URL (add
?v=2)
Import errors: Make sure you're using the exact versions specified:
- PGlite:
npm:@electric-sql/pglite@0.2.17 - nano-supabase:
https://raw.githubusercontent.com/filipecabaco/nano-supabase/main/dist/index.js