Use the TypeScript SDK
Install @pgb/sdk, create a client, run a first query, and find the calls for each service.
The TypeScript SDK works in Node 18 and later, Bun, Deno and edge runtimes. It has no runtime dependencies and needs only fetch.
Install
npm install @pgb/sdk
Create a client
import { createClient } from "@pgb/sdk";
// Browser or mobile app: your PUBLISHABLE key
const pgb = createClient("my-project", { key: "pgb_pub_..." });
// Server: a SECRET key. Never ship it to a browser.
const admin = createClient({ projectId: "my-project", apiKey: process.env.PGBHUB_SECRET_KEY });
createClient("my-project") talks to https://my-project.pgbhub.app. You can also pass endpoint, or set PGBHUB_PROJECT_ID, PGBHUB_ENDPOINT and PGBHUB_API_KEY in the environment.
Your first query
const users = pgb.table("users");
const created = await users.create({ name: "Alex", email: "[email protected]" });
const admins = await users.find({ role: "admin" }, { limit: 10, orderBy: "created_at", order: "DESC" });
const one = await users.findOne({ email: "[email protected]" });
const total = await users.count();
For anything else, run SQL: await pgb.db.query("SELECT now()").
What is in the box
| Namespace | Main methods |
|---|---|
pgb.table(name) |
find, findFirst, findOne, create, update, delete, count |
pgb.db |
query |
pgb.auth |
signUp, signIn, signOut, getUser, refreshSession, requestOtp, sendMagicLink, forgotPassword, resetPassword, two-factor and social login calls |
pgb.storage |
upload, list, delete, createSignedUrl, createBucket, setBucketVisibility |
pgb.mail |
send |
pgb.functions |
invoke, list, deploy, delete |
pgb.cron |
list, create, update, toggle, run, listRuns, delete |
pgb.jobs |
enqueue, get, list, cancel |
pgb.queues |
send, receive, ack, nack |
pgb.realtime |
broadcast, subscribe |
pgb.actions |
call actions by name, $register, $list, $deliveries |
Admin calls such as sending mail, creating buckets or deploying functions need the secret key.
Sign users in
const result = await pgb.auth.signUp({ email, password });
const login = await pgb.auth.signIn({ email, password });
if (login.mfa_required) {
await pgb.auth.verifyMfa({ challengeToken: login.challenge_token!, code: "123456" });
}
const user = await pgb.auth.getUser();
The SDK keeps the session in memory. Use getSession() and setSession() to store it where your app needs it. See Authentication in your project.
Errors, timeouts and retries
Every failure throws a PGBError with status, code, retryAfter (on rate limits) and a readable message. Requests time out after 30 seconds by default. Reads retry twice after network errors, timeouts, 429, 502, 503 and 504. Writes are never retried automatically.
Generate types
npx pgb pull --db my_database --out src/types/pgbhub.d.ts
npx pgb dev
pgb pull writes TypeScript types from your schema. pgb dev starts a local mock gateway on port 4000 for development. The CLI also has pgb init, pgb login and pgb inspect.
Good to know
- Use a publishable key in the browser and rely on row-level security.
- Keep the secret key in an environment variable on your server.
More guides in Console: APIs, SDKs & tools
The REST API: query your data over HTTPS
Read and write rows, run SQL and call actions with plain HTTP requests to your project address.
Use the Python SDK
Install pgbhub, create a sync or async client, run a first query, and use auth, mail, storage and actions.
Use the Go and Rust SDKs
Install the Go and Rust clients and run a first call with each, then find auth, mail, storage and actions.
Connect AI coding tools with the MCP server
Let tools such as Claude Code, Cursor and Windsurf inspect your schema and run your actions through the PGB MCP server.
Webhooks: receiving and sending
Receive webhooks in a function, send them from event rules, cron jobs, jobs and actions, and verify signatures.
API keys
Publishable and secret project keys, account-level keys, how to create, scope, rotate and revoke them.