Skip to content
Skip to content
PGB Learn

Add server logic with functions

What functions are for

Some logic does not belong in the browser: working with secrets, talking to other services, or doing several database steps safely. A function is a small piece of code that runs on demand next to your database, with a database connection ready to use.

Create your first function

  1. Open Functions in the sidebar. This is Functions Studio.
  2. Choose + Create Function.
  3. Function Name: note_summary. Use letters, numbers, dashes and underscores.
  4. Runtime: JavaScript (Node.js 20).
  5. Trigger Type: HTTP / Webhook.
  6. Leave Timeout (ms) at its default.
  7. Replace the code with:
export default async function handler(req, context) {
  const { db } = context;
  const rows = await db.query("SELECT count(*)::int AS total FROM notes");
  const name = req.query?.name || "there";
  return {
    status: 200,
    headers: { "Content-Type": "application/json" },
    body: { message: `Hello ${name}`, total_notes: rows[0].total },
  };
}
  1. Choose Deploy Function.

context.db is the ambient database client. You never paste connection details into code.

Test it

Choose Test on the function. In the Function Live Test Runner set Query Params (JSON) to {"name": "Ana"} and choose Run Function. You will see the Execution Response, how long it took, and the Console Output & Run Logs. The runner also shows ready-made snippets for cURL, JavaScript, Python and Axios.

Now call it from outside. Every function is available at https://<your-project>.pgbhub.app/functions/note_summary. From the SDK:

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

Use a secret

Say your function calls a service that needs an API key. Never put it in the code.

  1. In the sidebar, under Configuration, choose Secrets & Env.
  2. Choose + Add Secret, enter NOTIFY_API_KEY as the Secret Key Name and the key as the Secret Value, then Save & Encrypt Secret.
  3. In your function read it with context.env.NOTIFY_API_KEY.

The value is encrypted and shown as dots afterwards. Project secrets are also given to your containers.

Read the logs

Choose Logs to open Execution Logs. You see invocations, success rate, average duration and each run with its request, response and output. Because logs are stored in your database, you can switch the mode to Errors Only (Save Space) or Disabled (0 Storage), and use Purge Old Logs.

Webhooks and safety

A function with an HTTP trigger can be called by anyone who knows its address. If a service sends you webhooks, verify its signature in your code using a secret from Secrets & Env. Requests carrying PGB's X-PGBHub-Signature header are verified automatically, and a stale timestamp is refused to stop replays.

Limits to remember

  • The timeout can be set from half a second to 60 seconds.
  • A function that exceeds its memory limit is stopped and reports it.
  • Python functions can import only a short list of standard modules.

Recap

  • Functions run your code next to the database with context.db ready.
  • Deploy with Deploy Function, try it with Test, call it at /functions/<name>.
  • Keep keys in Secrets & Env and read them from env.
  • Check Execution Logs when something misbehaves.

Open Access Course

Free

forever
  • 9 structured lessons across 6 modules
  • Interactive knowledge checks with instant feedback
  • Verifiable completion certificate & PDF