Skip to content
Skip to content
PGB Learn

Create and deploy a function

Write a function in the Console editor, choose a runtime and trigger, test it, and call it over HTTP.

Updated Nov 12, 2026

A function is a small piece of server-side code that runs on demand, right next to your database. Use functions for webhooks, background logic, and anything your app should not do in the browser. They get a database connection automatically, so you never put connection details in code.

Open Functions

In your project's sidebar, choose Functions (under Core Services & Infrastructure). The page is called Functions Studio. It lists each function with its trigger and URL, Runtime, Invocations, Avg Latency and Last Status, with Test, Logs and Edit buttons.

Create a function

  1. Choose + Create Function (or start from a Quick Starter Template: REST API Endpoint, Stripe Webhook, Resend Email Notifier or Python Aggregator).
  2. Enter a Function Name: letters, numbers, dashes and underscores.
  3. Optionally add a Title / Label and Description.
  4. Pick a Runtime: JavaScript (Node.js 20), TypeScript or Python 3.11.
  5. Pick a Trigger Type: HTTP / Webhook, Database Table Event or Cron Schedule.
  6. Set a Timeout (ms).
  7. Write your code in the editor. There is a Fullscreen Studio and a Format button.
  8. Optionally add Environment Secrets (JSON). See Function secrets and logs.
  9. Choose Deploy Function.

What a function looks like

In JavaScript or TypeScript, export a default handler. It receives the request and a context that contains an ambient database client (db) and your secrets (env):

export default async function handler(req, context) {
  const { db } = context;
  const name = req.body?.name || req.query?.name || "friend";
  const rows = await db.query("SELECT now() AS server_time");
  return {
    status: 200,
    headers: { "Content-Type": "application/json" },
    body: { message: `Hello ${name}`, time: rows[0].server_time },
  };
}

In Python the handler is handler(event, context) and returns the same shape: status, headers, body. The database client is context.get("db").

Test it

Choose Test on the function. The Function Live Test Runner lets you set a Request Body (JSON) and Query Params (JSON), then Run Function. You see the Execution Response, how long it took, and the Console Output & Run Logs. The runner also gives you ready-made snippets for cURL, JavaScript, Python and Axios.

Call it from outside

Each function is available at https://<your-project>.pgbhub.app/functions/<name>. From the TypeScript SDK:

const result = await pgb.functions.invoke("hello", { name: "Alex" });

You can also deploy, list and delete functions from code: pgb.functions.deploy(name, code, options), list() and delete(name), with a secret key.

Edit and redeploy

Choose Edit, change the code and deploy again. Deployments are recorded and can be rolled back from the Deployments page in the sidebar.

Good to know

  • The timeout can be set from half a second to 60 seconds. A request that runs longer ends with a timeout error.
  • Each function has a memory limit (128 MB by default). A function that exceeds it is stopped and reports that it exceeded its memory limit.
  • HTTP calls also have a gateway safety limit on total time.
  • If your account is paused for non-payment, creating functions is refused with a clear message.
  • Functions share your project's database. Be careful with queries that change data, and test them first.

More guides in Console: functions, cron & jobs