Encore.ts lets you define backend services in TypeScript and automatically provisions the cloud infrastructure — databases, pub/sub, cron jobs, and caching.
Define an API
import { api } from 'encore.dev/api';
import { SQLDatabase } from 'encore.dev/storage/sqldb';
const db = new SQLDatabase('mydb', { migrations: './migrations' });
interface User { id: number; name: string; email: string; }
// GET /users/:id
export const getUser = api(
{ method: 'GET', path: '/users/:id', expose: true },
async ({ id }: { id: number }): Promise<User> => {
const row = await db.queryRow`SELECT * FROM users WHERE id = ${id}`;
return row as User;
}
);
// POST /users
export const createUser = api(
{ method: 'POST', path: '/users', expose: true },
async (params: { name: string; email: string }): Promise<User> => {
const row = await db.queryRow`
INSERT INTO users (name, email) VALUES (${params.name}, ${params.email})
RETURNING *`;
return row as User;
}
);
Pub/Sub
import { Topic, Subscription } from 'encore.dev/pubsub';
interface OrderEvent { orderId: string; userId: string; total: number; }
export const orderTopic = new Topic<OrderEvent>('orders');
// Publisher
export const placeOrder = api(
{ method: 'POST', path: '/orders' },
async (order: OrderEvent) => {
await orderTopic.publish(order);
return { status: 'queued' };
}
);
// Subscriber
new Subscription(orderTopic, 'process-order', {
handler: async (event: OrderEvent) => {
await processPayment(event);
await sendConfirmation(event);
}
});
Cron Jobs
import { CronJob } from 'encore.dev/cron';
new CronJob('daily-report', {
title: 'Generate daily report',
schedule: '0 9 * * *',
endpoint: generateReport
});
Caching
import { CacheCluster, CacheKeyspace } from 'encore.dev/storage/cache';
const cluster = new CacheCluster('main');
const userCache = new CacheKeyspace<User>(cluster, {
keyPattern: 'user/:id',
defaultExpiry: { minutes: 15 }
});
// Use in API
const cached = await userCache.get({ id: userId });
Why This Matters
- Zero DevOps: Infrastructure provisioned from code annotations
- Type-safe: Full TypeScript, no YAML configs
-
Local dev: Everything runs locally with
encore run - Auto-scaling: Deploy to Encore Cloud or self-host on AWS/GCP
Need custom backend tools or infrastructure automation? I build developer tools. Check out my web scraping actors on Apify or reach out at spinov001@gmail.com for custom solutions.
Top comments (0)