Skip to main content
This page is part of an AI coding agent skill and is written for agents, not humans. For the human-readable Base44 docs, see the developer documentation.

Creating Functions

Base44 functions are serverless backend functions. They are defined locally in your project and deployed to the Base44 backend.

Function Directory

All function definitions must be placed in the base44/functions/ folder in your project. The simplest function is a folder with an entry.ts or entry.js file inside it. Example structure:

How to Create a Function

  1. Create a new directory in base44/functions/ with your function name (use kebab-case)
  2. Create entry.ts (or entry.js) in that directory
  3. Deploy the function using the CLI

Function Discovery

The CLI discovers functions from entry.ts or entry.js files. A folder that contains one of those files is a function:
The function name is the path from the functions root to that folder. For example: Rules:
  • entry.ts or entry.js must be inside a named subfolder, not directly in base44/functions/
  • all *.js, *.ts, *.json, and *.jsonc files under the function folder are included when deploying
  • function paths with a dot in any path segment are ignored

Entry Point File

Functions export a default request handler. Use the npm: prefix to import npm packages.

Request Object

The function receives a standard Request object:
  • req.json() - Parse JSON body
  • req.text() - Get raw text body
  • req.headers - Access request headers
  • req.method - HTTP method

Response Object

Return using Response.json() for JSON responses:

Complete Example

Directory Structure

entry.ts

Using Service Role Access

For admin-level operations, use asServiceRole:

Using Secrets

Read secrets configured in the app dashboard with secrets.get() from the base44:runtime module. BASE44_APP_ID is pre-populated; set everything else in app settings → environment variables.

Post-Response Work

For work that should finish after the response is sent (analytics pings, non-critical logging), pass the promise to waitUntil() from base44:runtime. The response returns immediately and the function stays alive until the promise settles.

Naming Conventions

  • Directory name: Use kebab-case (e.g., process-order, send-notification)
  • Function name: Comes from the directory path under base44/functions/
    • Valid: process-order, orders/process, send_notification, myFunction
    • Invalid: process.order, send.notification.v2
  • Entry file: Use entry.ts or entry.js

Deploying Functions

After creating your function, deploy it to Base44:
For more details on deploying, see functions-deploy.md.

Notes

  • Use npm: prefix for npm packages (e.g., npm:@base44/sdk), always with a pinned version
  • Use createClientFromRequest(req) to get a client that inherits the caller’s auth context
  • Configure secrets via app dashboard for API keys, and read them with secrets.get() from base44:runtime
  • Crypto is the async Web Crypto API — for Stripe webhooks use await stripe.webhooks.constructEventAsync(...); the synchronous constructEvent() throws “SubtleCryptoProvider cannot be used in a synchronous context”
  • Make sure to handle errors gracefully and return appropriate HTTP status codes

Multi-File Functions

A function is not limited to entry.ts. Any .js, .ts, .json, or .jsonc file in the function’s folder is uploaded on deploy and can be imported from entry.ts with a relative path.
Rules:
  • Import files in the same folder with ./, including the extension (e.g. ./validate.ts).
  • The entire function folder ships on every deploy; anything the entry does not import is dropped from the bundle, so extra files are harmless.

Sharing Code Between Functions

To share code across functions, put it in base44/shared/ — the one directory outside a function folder that the CLI uploads. Its full contents are bundled with every function.
Rules:
  • Shared code must live in base44/shared/. Files elsewhere outside the function folder are not uploaded.
  • A relative import can reach a sibling (./util.ts) or base44/shared/ (../../shared/util.ts) — but nothing further out. An import that escapes base44/ (e.g. ../../../src/utils.ts) fails at deploy time; move the file into base44/shared/ instead.
  • For external packages use npm: specifiers, not relative paths.

Common Mistakes