• Blog
  • Docs
  • Pricing
  • We’re hiring!
Log inSign up
arfan

arfan

TEMPLATE

Public
Like
TEMPLATE
Home
Code
16
.claude
3
.opencode
1
.vscode
2
BACKEND
3
FRONTEND
3
SHARED
wiki
2
.gitignore
.vtignore
AGENTS.md
H
app.http.ts
biome.json
deno.json
justfile
justfile2
opencode.jsonc
Environment variables
Branches
1
Pull requests
Remixes
History
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.
Sign up now
Code
/
BACKEND
/
README.md
Code
/
BACKEND
/
README.md
Search
1/28/2026
Viewing readonly version of main branch: v51
View latest version
README.md

Backend Documentation

Overview

The backend of this project is built using the Hono framework for API routes, SQLite via Val Town's std/sqlite module for data persistence, and OAuth middleware for authentication.

Project Structure

BACKEND/ ├── README.md # This file ├── routes.users.ts # Hono API routes for user operations └── storage.users.ts # SQLite database operations for users

Naming Conventions

To maintain consistency and clarity across the backend codebase, follow these naming conventions:

Route Files

Route files should follow the pattern:

routes.{resource}.ts

Examples:

  • routes.users.ts - User-related API endpoints
  • routes.posts.ts - Post-related API endpoints
  • routes.comments.ts - Comment-related API endpoints

Storage Files

Storage files (database operations) should follow the pattern:

storage.{resource}.ts

Examples:

  • storage.users.ts - User database operations
  • storage.posts.ts - Post database operations
  • storage.comments.ts - Comment database operations

Current Files

routes.users.ts

Purpose: Hono API routes for user operations.

Key Features:

  • Exports a Hono router instance
  • Provides the /user endpoint (GET) to retrieve current user information
  • Integrates with OAuth middleware for authentication
  • Uses storage.users.ts for database operations

Endpoints:

MethodPathDescriptionAuth Required
GET/userGet current user infoYes

Example Response:

{ "user": { "id": 1, "email": "user@example.com", "username": "johndoe", "profile_image_url": "https://example.com/avatar.jpg" } }

storage.users.ts

Purpose: SQLite database operations for user management.

Key Features:

  • Manages the users_v1 table (configured in shared/config.js)
  • Provides functions for creating and retrieving user records
  • Automatically creates the users table if it doesn't exist
  • Uses Val Town's std/sqlite module

Exported Functions:

FunctionDescriptionParametersReturns
getOrCreateUserRetrieves an existing user by email or creates a new oneemail, username?, profileImageUrl?Promise<User>

User Schema:

ColumnTypeConstraints
idINTEGERPRIMARY KEY AUTOINCREMENT
emailTEXTUNIQUE NOT NULL
usernameTEXTOptional
profile_image_urlTEXTOptional
created_atTEXTDEFAULT CURRENT_TIMESTAMP
updated_atTEXTDEFAULT CURRENT_TIMESTAMP

Type Definition: See shared/types.ts for the User interface.

Technologies Used

Hono Framework

Hono is a lightweight, fast web framework for building API routes. It provides:

  • Expressive routing
  • Middleware support
  • TypeScript support
  • Minimal overhead

SQLite via Val Town std/sqlite

Val Town provides a hosted SQLite service via https://esm.town/v/std/sqlite/main.ts:

  • Serverless-compatible
  • Persistent storage across requests
  • Simple SQL execution interface

OAuth Middleware

Authentication is handled by Val Town's OAuth middleware (https://esm.town/v/std/oauth/middleware.ts):

  • Automatically handles /auth/login and /auth/logout routes
  • Provides getOAuthUserData() to retrieve session information
  • Integrates seamlessly with Val Town's authentication system

Adding New Resources

When adding a new resource to the backend:

  1. Create the storage file:

    • Name it storage.{resource}.ts
    • Define the database schema
    • Export functions for CRUD operations
  2. Create the routes file:

    • Name it routes.{resource}.ts
    • Import the storage functions
    • Define API endpoints using Hono
    • Export the router
  3. Update the main app:

    • Import the new router in app.http.ts
    • Mount it under the /api prefix
  4. Add types (if needed):

    • Update shared/types.ts with new interfaces
  5. Update configuration (if needed):

    • Add table names to shared/config.js

Best Practices

  • Table versioning: When modifying a table schema, create a new version (e.g., users_v2) rather than altering the existing table
  • Error handling: Let errors bubble up rather than catching and logging them; Hono's error handler will provide full stack traces
  • Type safety: Always use TypeScript interfaces for data structures
  • Authentication: Use getOAuthUserData() to check authentication status in protected routes
  • Environment variables: Use Deno.env.get() for secrets, though prefer APIs that don't require keys when possible
FeaturesVersion controlCode intelligenceCLIMCP
Use cases
TeamsAI agentsSlackGTM
DocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareers
We’re hiring!
Brandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Open Source Pledge
Terms of usePrivacy policyAbuse contact
© 2026 Val Town, Inc.