DEV Community

Alex Spinov
Alex Spinov

Posted on

Encore Has a Free API You Should Know About

Encore is a backend framework that generates infrastructure from your code — type-safe APIs, databases, cron jobs, all declared in code.

Define an API — TypeScript

// backend/user/user.ts
import { api } from 'encore.dev/api'
import { SQLDatabase } from 'encore.dev/storage/sqldb'

const db = new SQLDatabase('users', { migrations: './migrations' })

interface User {
  id: number
  name: string
  email: string
}

export const list = api(
  { expose: true, method: 'GET', path: '/users' },
  async (): Promise<{ users: User[] }> => {
    const rows = await db.query`SELECT * FROM users ORDER BY id`
    const users: User[] = []
    for await (const row of rows) {
      users.push({ id: row.id, name: row.name, email: row.email })
    }
    return { users }
  }
)

export const create = api(
  { expose: true, method: 'POST', path: '/users' },
  async (params: { name: string; email: string }): Promise<User> => {
    const row = await db.queryRow`
      INSERT INTO users (name, email) VALUES (${params.name}, ${params.email})
      RETURNING id, name, email
    `
    return row!
  }
)
Enter fullscreen mode Exit fullscreen mode

Define an API — Go

package user

import "context"

type User struct {
    ID    int    `json:"id"`
    Name  string `json:"name"`
    Email string `json:"email"`
}

//encore:api public method=GET path=/users
func List(ctx context.Context) (*ListResponse, error) {
    rows, err := sqldb.Query(ctx, "SELECT id, name, email FROM users")
    if err != nil { return nil, err }
    defer rows.Close()

    var users []User
    for rows.Next() {
        var u User
        rows.Scan(&u.ID, &u.Name, &u.Email)
        users = append(users, u)
    }
    return &ListResponse{Users: users}, nil
}
Enter fullscreen mode Exit fullscreen mode

Pub/Sub — Event-Driven

import { Topic, Subscription } from 'encore.dev/pubsub'

interface OrderEvent {
  orderId: string
  userId: string
  total: number
}

export const OrderCreated = new Topic<OrderEvent>('order-created', {
  deliveryGuarantee: 'at-least-once'
})

// Publish
await OrderCreated.publish({ orderId: '123', userId: 'u1', total: 99 })

// Subscribe
const _ = new Subscription(OrderCreated, 'send-confirmation', {
  handler: async (event) => {
    await sendEmail(event.userId, `Order ${event.orderId} confirmed!`)
  }
})
Enter fullscreen mode Exit fullscreen mode

Cron Jobs

import { CronJob } from 'encore.dev/cron'

const _ = new CronJob('daily-cleanup', {
  title: 'Clean up expired sessions',
  every: '24h',
  endpoint: cleanupSessions
})

export const cleanupSessions = api(
  { expose: false },
  async () => {
    await db.exec`DELETE FROM sessions WHERE expires_at < NOW()`
  }
)
Enter fullscreen mode Exit fullscreen mode

Auto-Generated Architecture Diagram

Encore understands your code and generates live architecture diagrams, API documentation, and distributed tracing — automatically.

Real-World Use Case

A team was spending 40% of their time on infrastructure: Terraform for AWS, Docker for local dev, manual API documentation, hand-written pub/sub configuration. They rewrote their 5 microservices with Encore: infrastructure is derived from code, API docs are auto-generated, local dev works instantly. Infrastructure management time: near zero.

Encore is Infrastructure-from-Code — the next evolution after Infrastructure-as-Code.


Build Smarter Data Pipelines

Need to scrape websites, extract APIs, or automate data collection? Check out my ready-to-use scrapers on Apify — no coding required.

Custom scraping solution? Email me at spinov001@gmail.com — fast turnaround, fair prices.

Top comments (0)