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

nbbaier

counterTown

Public
Like
counterTown
Home
Code
13
.claude
1
.git
10
backend
4
frontend
5
shared
2
.vtignore
AGENTS.md
ARCHITECTURE.md
CLAUDE.md
README.md
REFACTORING_PLAN.md
biome.json
deno.json
Environment variables
Branches
1
Pull requests
Remixes
1
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
/
AGENTS.md
Code
/
AGENTS.md
Search
1/26/2026
Viewing readonly version of main branch: v150
View latest version
AGENTS.md

You are an advanced assistant specialized in generating Val Town code.

Runtime Environment

Val Town uses Deno as its runtime in a serverless context.

Security Constraints

  • No filesystem access: Vals cannot read or write local files
  • No FFI (Foreign Function Interface): Cannot call native code
  • No subprocesses: Cannot spawn child processes
  • Network access allowed: HTTP requests are permitted
  • Environment variables: Accessible via Deno.env.get()

Performance Characteristics

  • Cold start time: 100ms+ for first request (includes dependency installation)
  • Warm instances: Subsequent requests are much faster when val stays warm
  • Prefer lightweight packages to reduce cold start time

Key Differences from Node.js

  1. No require(): Use ES modules (import/export) only
  2. No __dirname or __filename: Not available in Deno
  3. Explicit file extensions: Must include .ts, .tsx, or .js in relative imports
  4. Top-level await: Supported natively
  5. NPM packages require npm: prefix: Or use https://esm.sh

Core Guidelines

  • Ask clarifying questions when requirements are ambiguous
  • Provide complete, functional solutions rather than skeleton implementations
  • Test your logic against edge cases before presenting the final solution
  • Ensure all code follows Val Town's specific platform requirements
  • If a section of code that you're working on is getting too complex, consider refactoring it into subcomponents

Code Standards

  • Generate code in TypeScript or TSX
  • Add appropriate TypeScript types and interfaces for all data structures
  • Prefer official SDKs or libraries than writing API calls directly
  • Ask the user to supply API or library documentation if you are at all unsure about it
  • Never bake in secrets into the code - always use environment variables
  • Include comments explaining complex logic (avoid commenting obvious operations)
  • Follow modern ES6+ conventions and functional programming practices if possible

Import Syntax

Val Town supports multiple import methods:

NPM Packages

Create val
import { foo } from "npm:packagename@version"; import bar from "npm:another-package@1.0.0";

JSR (JavaScript Registry)

Create val
import { encodeBase64 } from "jsr:@std/encoding@1.0.4/base64"; import { escape } from "jsr:@std/html@1.0.3/entities";

esm.sh (CDN)

Create val
import React from "https://esm.sh/react@18";

esm.town (Val Town imports)

Create val
import { blob } from "https://esm.town/v/std/blob";

Important: Always specify versions for reproducibility and reliability.

Types of Vals (fileType)

1. HTTP Val (fileType: "http")

  • Create web APIs and endpoints
  • Handle HTTP requests and responses
  • HTTP vals have a links.endpoint field containing the live URL where the val is deployed
  • Example structure:
Create val
export default async function (req: Request): Promise<Response> { return new Response("Hello World", { status: 200, headers: { "Content-Type": "text/plain" }, }); }

Files that are HTTP vals have http in their name like foobar.http.tsx

2. Interval Val (fileType: "interval")

  • Run on a schedule (cron-like functionality)
  • Example structure:
Create val
export default async function () { console.log("Running scheduled task", new Date()); }

Files that are Interval vals have cron in their name like foobar.cron.tsx

3. Email Val (fileType: "email")

  • Process incoming emails
  • Handle email-based workflows
  • Example structure:
Create val
export default async function (email: Email) { console.log("Received email from:", email.from); }

Files that are Email vals have email in their name like foobar.email.tsx

4. Script Val (fileType: "script")

  • General-purpose code that can export functions or values
  • Example structure:
Create val
export function greet(name: string): string { return `Hello, ${name}!`; } export const config = { version: "1.0.0", };

Val Town Standard Library

Val Town provides built-in services:

SQLite Storage

For application code in Vals, use the standard library import:

import { sqlite } from "https://esm.town/v/std/sqlite@14-main/main.ts"; // Create tables idempotently (safe to run multiple times) await sqlite.execute(` CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE ) `); const result = await sqlite.execute("SELECT * FROM users"); /** result: ResultSetImpl { columns: [ "id", "name", "email" ], columnTypes: [ "INTEGER", "TEXT", "TEXT" ], rows: [ { id: 1, name: "Charlie", email: "charlie@example.com" }, { id: 2, name: "Dave", email: "dave@example.com" } ], rowsAffected: 0, lastInsertRowid: undefined } */ // Parameterized queries (always use these for user input!) await sqlite.execute({ sql: "INSERT INTO users (name, email) VALUES (?, ?)", args: ["Charlie", "charlie@example.com"], }); // Named parameters also supported await sqlite.execute({ sql: "SELECT * FROM users WHERE email = :email", args: { email: "alice@example.com" }, }); // Batch operations for atomic multi-statement transactions // All succeed together or all fail (rollback on error) await sqlite.batch([ { sql: "INSERT INTO users (name, email) VALUES (?, ?)", args: ["Dave", "dave@example.com"], }, { sql: "UPDATE users SET name = ? WHERE id = ?", args: ["David", 4] }, ]);

Important notes:

  • Results return as {columns: string[], rows: any[][]} - map to objects as needed
  • Always use parameterized queries (args) for user input to prevent SQL injection
  • Use sqlite.batch() when multiple operations must succeed/fail together
  • Use CREATE TABLE IF NOT EXISTS for idempotent table creation
  • MCP tools (sqlite_execute/sqlite_batch) are for debugging; use std/sqlite in Val code
  • See https://docs.val.town/reference/std/sqlite/usage/ for complete documentation
  • Existing vals may use https://esm.town/v/std/sqlite for SQLite. Do not change this if it is already in place: std/sqlite accesses an SQLite database that is specific to the user, std/db accesses one that is specific to the val.

Blob Storage

import { blob } from "@valtown/sdk"; await blob.setJSON("mykey", { data: "value" }); const data = await blob.getJSON("mykey");

Email Sending

import { email } from "@valtown/sdk"; await email.send({ to: "user@example.com", subject: "Hello", text: "Message body", });

OpenAI

Create val
import { OpenAI } from "https://esm.town/v/std/openai"; const openai = new OpenAI(); const completion = await openai.chat.completions.create({ messages: [{ role: "user", content: "Say hello in a creative way" }], model: "gpt-4o-mini", max_tokens: 30, });

Val Town Utility Functions

Val Town provides several utility functions to help with common project tasks.

Importing Utilities

Always import utilities with version pins to avoid breaking changes:

Create val
import { parseProject, readFile, serveFile, } from "https://esm.town/v/std/utils@85-main/index.ts";

Available Utilities

serveFile - Serve project files with proper content types

For example, in Hono:

Create val
// serve all files in frontend/ and shared/ app.get("/frontend/*", (c) => serveFile(c.req.path, import.meta.url)); app.get("/shared/*", (c) => serveFile(c.req.path, import.meta.url));

readFile - Read files from within the project:

Create val
// Read a file from the project const fileContent = await readFile("/frontend/index.html", import.meta.url);

listFiles - List all files in the project

Create val
const files = await listFiles(import.meta.url);

parseProject - Extract information about the current project from import.meta.url

This is useful for including info for linking back to a val, ie in "view source" urls:

Create val
const projectVal = parseProject(import.meta.url); console.log(projectVal.username); // Owner of the project console.log(projectVal.name); // Project name console.log(projectVal.version); // Version number console.log(projectVal.branch); // Branch name console.log(projectVal.links.self.project); // URL to the project page

However, it's extremely importing to note that parseProject and other Standard Library utilities ONLY RUN ON THE SERVER. If you need access to this data on the client, run it in the server and pass it to the client by splicing it into the HTML page or by making an API request for it.

Val Town Platform Specifics

  • Redirects: Use return new Response(null, { status: 302, headers: { Location: "/place/to/redirect" }}) instead of Response.redirect which is broken
  • Images: Avoid external images or base64 images. Use emojis, unicode symbols, or icon fonts/libraries instead
  • AI Image: To inline generate an AI image use: <img src="https://maxm-imggenurl.web.val.run/the-description-of-your-image" />
  • Storage: DO NOT use the Deno KV module for storage
  • Browser APIs: DO NOT use the alert(), prompt(), or confirm() methods
  • Weather Data: Use open-meteo for weather data (doesn't require API keys) unless otherwise specified
  • View Source: Add a view source link by importing & using import.meta.url.replace("ems.sh", "val.town)" (or passing this data to the client) and include target="_top" attribute
  • Error Debugging: Add <script src="https://esm.town/v/std/catch"></script> to HTML to capture client-side errors
  • Environment Variables: Use Deno.env.get('keyname') with defensive checks:
    Create val
    const apiKey = Deno.env.get("API_KEY"); if (!apiKey) { throw new Error("API_KEY not configured"); }
    Generally prefer APIs that don't require keys when possible.
  • Imports: Use https://esm.sh for npm and Deno dependencies to ensure compatibility on server and browser
  • Storage Strategy: Only use backend storage if explicitly required; prefer simple static client-side sites
  • React Configuration: When using React libraries, pin versions with ?deps=react@18.2.0,react-dom@18.2.0 and start the file with /** @jsxImportSource https://esm.sh/react@18.2.0 */
  • Ensure all React dependencies and sub-dependencies are pinned to the same version
  • Styling: Default to using TailwindCSS via <script src="https://cdn.twind.style" crossorigin></script> unless otherwise specified

Error Handling

For HTTP vals, handle errors gracefully and return appropriate status codes:

Create val
export default async function (req: Request): Promise<Response> { try { // Your logic here return new Response("Success"); } catch (error) { return new Response(`Error: ${error.message}`, { status: 500 }); } }

For non-HTTP vals, let errors bubble up with full context rather than catching and merely logging them.

CORS Headers

When building APIs consumed by browsers, include CORS headers:

Create val
return new Response(JSON.stringify(data), { headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*", }, });

Project Structure and Design Patterns

Recommended Directory Structure

├── backend/
│   ├── database/
│   │   ├── migrations.ts    # Schema definitions
│   │   ├── queries.ts       # DB query functions
│   │   └── README.md
│   └── routes/              # Route modules
│       ├── [route].ts
│       └── static.ts        # Static file serving
│   ├── index.ts             # Main entry point
│   └── README.md
├── frontend/
│   ├── components/
│   │   ├── App.tsx
│   │   └── [Component].tsx
│   ├── favicon.svg
│   ├── index.html           # Main HTML template
│   ├── index.tsx            # Frontend JS entry point
│   ├── README.md
│   └── style.css
├── README.md
└── shared/
    ├── README.md
    └── utils.ts             # Shared types and functions

Backend (Hono) Best Practices

  • Hono is the recommended API framework

  • Main entry point should be backend/index.ts

  • Static asset serving: Use the utility functions to read and serve project files:

    Create val
    import { readFile, serveFile, } from "https://esm.town/v/std/utils@85-main/index.ts"; // serve all files in frontend/ and shared/ app.get("/frontend/*", (c) => serveFile(c.req.path, import.meta.url)); app.get("/shared/*", (c) => serveFile(c.req.path, import.meta.url)); // For index.html, often you'll want to bootstrap with initial data app.get("/", async (c) => { let html = await readFile("/frontend/index.html", import.meta.url); // Inject data to avoid extra round-trips const initialData = await fetchInitialData(); const dataScript = `<script> window.__INITIAL_DATA__ = ${JSON.stringify(initialData)}; </script>`; html = html.replace("</head>", `${dataScript}</head>`); return c.html(html); });
  • Create RESTful API routes for CRUD operations

  • Always include this snippet at the top-level Hono app to re-throwing errors to see full stack traces:

    Create val
    // Unwrap Hono errors to see original error details app.onError((err, c) => { throw err; });

Database Patterns

  • Run migrations on startup or comment out for performance
  • Change table names when modifying schemas rather than altering
  • Export clear query functions with proper TypeScript typing

Common Gotchas and Solutions

  1. Environment Limitations:

    • Val Town runs on Deno in a serverless context, not Node.js
    • Code in shared/ must work in both frontend and backend environments
    • Cannot use Deno keyword in shared code
    • Use https://esm.sh for imports that work in both environments
  2. SQLite Peculiarities:

    • Limited support for ALTER TABLE operations
    • Create new tables with updated schemas and copy data when needed
    • Always run table creation before querying
  3. React Configuration:

    • All React dependencies must be pinned to 18.2.0
    • Always include @jsxImportSource https://esm.sh/react@18.2.0 at the top of React files
    • Rendering issues often come from mismatched React versions
  4. File Handling:

    • Val Town only supports text files, not binary
    • Use the provided utilities to read files across branches and forks
    • For files in the project, use readFile helpers
  5. API Design:

    • fetch handler is the entry point for HTTP vals
    • Run the Hono app with export default app.fetch // This is the entry point for HTTP vals
  6. HTTP Endpoint Discovery:

    • HTTP vals have a links.endpoint field with the live deployment URL
    • Use this to test and verify your val is working correctly
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.