The Neon serverless driver (Documentation Index
Fetch the complete documentation index at: https://planetscale.com/docs/llms.txt
Use this file to discover all available pages before exploring further.
@neondatabase/serverless) runs Postgres queries from JavaScript and TypeScript over HTTP or WebSockets instead of TCP. PlanetScale supports this driver on platforms where standard TCP drivers and connection pooling are not viable.
For full API details, see Neon’s serverless driver documentation and the GitHub repository.
Driver v1.0.0 and later require Node.js 19 or higher.
When to use this driver
Use the Neon serverless driver when your deployment platform cannot maintain TCP connections or a connection pool between requests.| Platform | Recommended approach |
|---|---|
| Netlify Functions | Neon serverless driver (this page) |
| Deno Deploy | Neon serverless driver (this page) |
| Cloudflare Workers (without Hyperdrive) | Neon serverless driver (this page) |
| Vercel | pg with @vercel/functions and PgBouncer on port 6432 |
| Cloudflare Workers + Hyperdrive | pg with Hyperdrive |
| AWS Lambda, Railway, Render, VPS, Docker | pg with PgBouncer on port 6432 |
Vercel
Vercel Fluid compute reuses warm function instances, making TCP connection pooling safe. For Vercel deployments, connect withpg through PgBouncer on port 6432 and pool connections with attachDatabasePool from @vercel/functions.
The Neon serverless driver remains an option on Vercel when you cannot use connection pooling (for example, classic serverless without Fluid). See Neon’s Vercel connection guide for a comparison of TCP, HTTP, and WebSocket latency on that platform.
Cloudflare Workers
For Cloudflare Workers, Hyperdrive withpg is the recommended approach. It provides connection pooling and lower latency for warm connections. Use the Neon serverless driver only when Hyperdrive is not available for your setup.
Deno Deploy
Install the driver from JSR for Deno projects:HTTP vs WebSocket modes
The Neon serverless driver supports two connection modes:- HTTP mode — Uses the
neonfunction to execute queries over HTTP. Faster for single queries and non-interactive transactions. No connection state is maintained between requests. - WebSocket mode — Uses the
Poolobject to establish a WebSocket connection. Required for session support, interactive transactions, ornode-postgrescompatibility.
| Use case | Recommended mode |
|---|---|
| Single queries | HTTP |
| Non-interactive transactions (batch of queries) | HTTP |
| Interactive transactions | WebSocket |
| Session-based features | WebSocket |
node-postgres compatibility | WebSocket |
Setting up credentials
Both modes use the same credentials setup.You’ll need to create a Postgres role to use with the driver.
Once you have these credentials, place them in environment variables:These can all be added to a unified Postgres connection URL for use by the driver:
Using HTTP mode
HTTP mode is the simplest way to execute queries. You must configure thefetchEndpoint to use PlanetScale’s SQL endpoint.
neon function returns a tagged template literal that automatically handles parameterized queries, protecting against SQL injection. For manually parameterized queries, use sql.query():
arrayMode, fullResults, fetchOptions, and other configuration options.
Non-interactive transactions
HTTP mode supports non-interactive transactions where you send a batch of queries to be executed together. Use thetransaction function:
Using WebSocket mode
WebSocket mode provides a fullPool interface compatible with the pg library. See Neon’s WebSocket documentation for the full API reference. This mode requires additional configuration for PlanetScale connections.
In browser or edge environments that have a native
WebSocket global, you don’t need to import ws or set neonConfig.webSocketConstructor.Security
PlanetScale requiresSCRAM-SHA-256 for all authentication to Postgres servers.
We maintain this strict requirement for security purposes.
For WebSocket connections, you must set neonConfig.pipelineConnect = false;.
This adds a bit of additional latency, but is necessary to connect using SCRAM-SHA-256.
When this is "password" (the default value) it requires using cleartext password authentication, reducing connection security.
HTTP mode connections handle authentication automatically and don’t require this configuration.
