Skip to content

Overview

functions serverless lambda API endpoints backend webhooks event triggers TypeScript JavaScript routing deployment environment variables

Nhost Functions are server-side JavaScript or TypeScript functions that are deployed as HTTP endpoints. They are a great option for handling Event Triggers, integrating with external services like Stripe or Slack, and building custom API logic.

Each .js or .ts file you place in the ./functions directory of your project becomes an HTTP endpoint. A function exports a default handler that receives an Express Request and Response:

import type { Request, Response } from 'express'
export default (req: Request, res: Response) => {
res.status(200).send('Hello World!')
}

Endpoints are generated based on the file path relative to ./functions:

FileHTTP Endpoint
functions/index.jshttps://[subdomain].functions.[region].nhost.run/v1/
functions/users/index.tshttps://[subdomain].functions.[region].nhost.run/v1/users
functions/users/active.tshttps://[subdomain].functions.[region].nhost.run/v1/users/active
functions/my-company.jshttps://[subdomain].functions.[region].nhost.run/v1/my-company

All HTTP methods (GET, POST, PUT, DELETE, etc.) are routed to the same handler. Use req.method to differentiate.

  • Files and folders prefixed with an underscore (_) are ignored and not exposed as endpoints. Use this for shared utilities.
  • index files map to the parent directory path.

Functions receive standard Express Request and Response objects with a few additions:

PropertyDescription
req.methodHTTP method (GET, POST, etc.)
req.headersRequest headers
req.queryParsed query string parameters
req.bodyParsed request body (JSON, form data)
req.rawBodyRaw request body as a Buffer
req.invocationIdUnique identifier for this function invocation

The invocationId is useful for correlating logs across a single invocation.

The following environment variables are automatically injected into your functions at runtime:

VariableDescription
NHOST_ADMIN_SECRETAdmin secret for your project
NHOST_WEBHOOK_SECRETWebhook secret for your project
NHOST_JWT_SECRETJWT secret configuration (JSON string)
NHOST_SUBDOMAINProject subdomain
NHOST_REGIONProject region
NHOST_HASURA_URLInternal Hasura URL
NHOST_AUTH_URLInternal Auth URL
NHOST_GRAPHQL_URLInternal GraphQL URL
NHOST_STORAGE_URLInternal Storage URL
NHOST_FUNCTIONS_URLInternal Functions URL

Any custom environment variables you define are also available.

Functions are deployed automatically when you push to the connected GitHub repository. Nhost computes checksums for your functions and their dependencies, so unchanged functions are skipped during deployment.

Nhost detects your package manager based on the lockfile present in the functions folder or a parent folder. See Runtimes and Dependencies for details.

Functions are billed per GB-sec or GB-hour. 1 GB-hour is 3600 GB-seconds.

1 GB-sec is 1 function with 1 GB of RAM running for 1 second. If 1 function with 1 GB of RAM runs for 3600 seconds, that equals 1 GB-hour.