> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://help.hodi.host/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# How to create serverless functions?

Add an `api/` directory to any static Hodifly project and every JavaScript file in it becomes an HTTP endpoint under `/api/` on your own domain.

## Routing

One file, one URL. The file's path decides the endpoint:

| File | URL |
| ---- |
| `api/hello.js` | `/api/hello` |
| `api/contact/index.js` | `/api/contact` |
| `api/user/[id].js` | `/api/user/42` (the `id` parameter receives `"42"`) |

Literal paths win over `[bracket]` parameters, and `.js`, `.mjs` and `.cjs` files all work. TypeScript and Python functions are not supported at this point: they answer 404, and the deploy log warns about them.

## Writing a function

Both common handler styles are supported, in the same project if you like.

Classic signature (Express-like):

```js
// api/hello.js
export default function handler(req, res) {
  // req.query, req.body (JSON and form bodies are parsed), req.cookies
  res.status(200).json({ hello: "world" });
}
```

Web standard signature:

```js
// api/user/[id].js
export function GET(request) {
  return Response.json({ ok: true });
}
```

`module.exports = (req, res) => ...`, per-method exports (`GET`, `POST`, ...) with automatic 405 for the rest, and `export default { fetch(request) {...} }` all work. Functions can `import` packages from your `package.json`: dependencies installed for the build are available at runtime.

## Environment variables and secrets

The project's environment variables (Advanced settings, encrypted at rest, never sent to Hodi) are available in your functions as `process.env.MY_VAR`. This is the right place for API keys: function source is never served as a static file, and requesting a source path such as `/api/hello.js` answers 404.