• Townie
    AI
  • Blog
  • Docs
  • Pricing
  • We’re hiring!
Log inSign up
yawnxyz

yawnxyz

mockingbird

Public
Like
mockingbird
Home
Code
10
backend
1
core
4
frontend
1
.gitignore
.vtignore
AGENTS.md
EXAMPLE.md
README.md
deno.json
main.js
Branches
1
Pull requests
Remixes
History
Environment variables
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
/
EXAMPLE.md
Code
/
EXAMPLE.md
Search
…
EXAMPLE.md

Example: Adding a Counter API

This example shows how to add a new API endpoint with backend logic.

1. Create a Backend Service

// backend/services/counter.js let count = 0; export function increment() { count++; return count; } export function getCount() { return count; } export function reset() { count = 0; return count; }

2. Add Routes

In core/routes.js, add:

import { increment, getCount, reset } from '../backend/services/counter.js'; // Inside setupRoutes function: app.get('/api/counter', (c) => { return c.json({ count: getCount() }); }); app.post('/api/counter/increment', (c) => { return c.json({ count: increment() }); }); app.post('/api/counter/reset', (c) => { return c.json({ count: reset() }); });

3. Update Frontend

In frontend/index.html, add Alpine.js code:

<div x-data="{ serverCount: 0, async fetchCount() { const res = await fetch('/api/counter'); const data = await res.json(); this.serverCount = data.count; }, async incrementServer() { const res = await fetch('/api/counter/increment', { method: 'POST' }); const data = await res.json(); this.serverCount = data.count; } }" x-init="fetchCount()"> <div>Server count: <span x-text="serverCount"></span></div> <button @click="incrementServer()">Increment on server</button> </div>

Result

Now you have a counter that persists on the server side!

FeaturesVersion controlCode intelligenceCLI
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
© 2025 Val Town, Inc.