DEV Community

Alex Spinov
Alex Spinov

Posted on

Neon Has a Free API — Serverless Postgres with Branching and Scale to Zero

Neon is serverless Postgres that scales to zero, branches like Git, and has a generous free tier. Your database sleeps when idle and wakes in milliseconds — perfect for serverless apps.

Why Neon?

  • Scale to zero — no compute charges when idle
  • Database branching — clone your entire DB for testing in seconds
  • 0.5s cold start — instant wake from sleep
  • Free tier — 512MB storage, 190 compute hours/month

Quick Start

  1. Sign up at neon.tech
  2. Create a project
  3. Copy the connection string
import { neon } from '@neondatabase/serverless';

const sql = neon(process.env.DATABASE_URL!);

const result = await sql`SELECT * FROM users WHERE id = ${userId}`;
Enter fullscreen mode Exit fullscreen mode

With Drizzle ORM

import { drizzle } from 'drizzle-orm/neon-http';
import { neon } from '@neondatabase/serverless';

const client = neon(process.env.DATABASE_URL!);
const db = drizzle(client);

const users = await db.select().from(usersTable);
Enter fullscreen mode Exit fullscreen mode

Connection Pooling (for long-running servers)

import { Pool } from '@neondatabase/serverless';

const pool = new Pool({ connectionString: process.env.DATABASE_URL });

const { rows } = await pool.query('SELECT * FROM users');
Enter fullscreen mode Exit fullscreen mode

Branching

# Install CLI
brew install neonctl

# Create branch
neonctl branches create --name feature-auth

# Get connection string for branch
neonctl connection-string feature-auth

# Delete branch
neonctl branches delete feature-auth
Enter fullscreen mode Exit fullscreen mode

Use branches for:

  • Preview environments — each PR gets its own database
  • Testing migrations — test against a copy, not production
  • Development — each developer gets their own branch

Neon API

# Create branch via API
curl -X POST 'https://console.neon.tech/api/v2/projects/{project_id}/branches' \
  -H 'Authorization: Bearer $NEON_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{"branch": {"name": "preview-123"}}'

# List branches
curl 'https://console.neon.tech/api/v2/projects/{project_id}/branches' \
  -H 'Authorization: Bearer $NEON_API_KEY'
Enter fullscreen mode Exit fullscreen mode

With Next.js (App Router)

// app/api/users/route.ts
import { neon } from '@neondatabase/serverless';

const sql = neon(process.env.DATABASE_URL!);

export async function GET() {
  const users = await sql`SELECT * FROM users ORDER BY created_at DESC`;
  return Response.json(users);
}

export async function POST(request: Request) {
  const { name, email } = await request.json();
  const [user] = await sql`
    INSERT INTO users (name, email) VALUES (${name}, ${email})
    RETURNING *
  `;
  return Response.json(user, { status: 201 });
}
Enter fullscreen mode Exit fullscreen mode

Need a database for your scraped data? Check out my Apify actors for web scraping + Neon storage, or email spinov001@gmail.com for custom data pipelines.

Neon, Supabase, or PlanetScale — which serverless DB do you use? Share below!

Top comments (0)