Xata implements the Neon serverless driver protocol, allowing you to use the
@neondatabase/serverless npm package to connect to your Xata database over HTTP or WebSocket. If you’re migrating from Neon, and you are using the serverless driver, you don’t need any code changes.Why use the Serverless Proxy?
Traditional PostgreSQL connections use TCP, which requires maintaining a persistent connection. This works well for long-running servers but presents challenges in serverless environments:- Cold starts: Serverless functions spin up and down frequently, making persistent connections impractical
- Edge locations: Edge functions often have restrictions on outbound TCP connections
- Connection limits: Each serverless invocation could exhaust your database connections
Connection methods
Xata supports three ways to connect to your database:| Method | Protocol | Best for |
|---|---|---|
| Standard | TCP (wire protocol) | Traditional servers, long-running applications |
| HTTP | HTTPS | Serverless functions, one-off queries |
| WebSocket | WSS | Edge functions, interactive applications |
Get started
Install the@neondatabase/serverless driver:
DATABASE_URL environment variable to your connection string. You can obtain it by running xata branch url or by copying the connection string from the Xata console:
HTTP vs WebSocket
The driver supports two modes of communication, each suited to different use cases:| Feature | HTTP | WebSocket |
|---|---|---|
| Connection overhead | None (stateless) | One-time handshake |
| Latency per query | Higher (new request each time) | Lower (reuses connection) |
| Transactions | Not supported | Supported |
| Session variables | Not supported | Supported |
| Prepared statements | Not supported | Supported |
| Multiple queries | One per request | Batched efficiently |
| Best for | Single, simple queries | Complex operations |
- You only need to run a single query per request
- Your queries are simple SELECTs or single INSERTs
- You want the simplest possible setup with zero connection management
- You need to run transactions (
BEGIN/COMMIT/ROLLBACK) - You’re executing multiple queries in a single request
- You need session-level features like
SETcommands or prepared statements - You want lower per-query latency for multiple operations
HTTP connections
Use HTTP for simple, stateless queries. Each query is a single HTTP request—no connection management required.HTTP connections are stateless. Each query runs in its own transaction and cannot share state with other queries. Use WebSocket connections if you need transactions or session-level features.
WebSocket connections
Use WebSocket connections when you need transactions, session variables, or want to execute multiple queries efficiently. The WebSocket mode maintains a persistent connection, allowing for stateful interactions with the database.If you are running in Node.js versions earlier than 21, you need to supply a WebSocket implementation. Install the This is not needed in edge runtimes (Vercel Edge, Cloudflare Workers, Deno) or Node.js 21+, which have built-in WebSocket support.
ws package (npm install ws) and configure it before creating a pool:Framework examples
- Vercel
- Cloudflare Workers
- Netlify
- AWS Lambda