---
title: Write stateless lunas
description: Define a luna type with no key and no data, whose handlers run many calls at once and reach keyed lunas with ctx.call.
related:
  - /lunas/call-another-luna
  - /http/serve-http-routes
  - /sdk/define-luna
---

Some work belongs to no single luna, such as an endpoint that gathers answers from many lists. Giving it to one keyed luna puts every caller's work on that luna's one thread.

A stateless luna type has no key and no data of its own. Many of its calls run at the same time, and it reaches keyed lunas with `ctx.call`.

## Define a stateless luna type

Give [`defineLuna`](/sdk/define-luna) `stateless: true` and no key.

```ts luna/lunas/api.ts
import { defineLuna, t } from "luna";
import { List } from "./list.ts";

export const Api = defineLuna({
  name: "Api",
  stateless: true,
  visibility: "public",
  handlers: (h) => ({
    counts: h({ args: { lists: t.array(t.string()) } }, async (ctx, { lists }) => {
      const all = await Promise.all(lists.map((id) => ctx.call(List, { id }).all({})));
      return lists.map((list, i) => ({ list, count: all[i].length }));
    }),
  }),
});
```

Each `List` still runs its own calls one at a time. The stateless type only asks them, side by side.

## Call it

Over HTTP, a stateless type's URL has no key: the type's name, then the handler's.

```bash
curl -X POST "$LUNA_URL/Api/counts" \
  -H "luna-key: $LUNA_KEY" \
  -H "Content-Type: application/json" \
  -d '{"lists":["groceries","chores"]}'
```

From another luna, call it with the key `{}`.

```ts luna/lunas/board.ts
summary: h({ args: { lists: t.array(t.string()) } }, (ctx, { lists }) => ctx.call(Api, {}).counts({ lists })),
```

A stateless handler can also answer an HTTP route of your choosing. See [Serve HTTP routes](/http/serve-http-routes).

## Know what it can have

A stateless type takes these options.

| Option | Does |
| --- | --- |
| `name` | The type's name, starting with a capital letter |
| `stateless` | `true` |
| `visibility` | `"private"` by default, or `"public"` for apps with a publishable key |
| `internet` | Set to `false` to keep its handlers from reaching the internet |
| `limits` | Ceilings on `memory` and `cpu`, as a keyed type has them, without `reserve` |
| `isolates` | Templates for running JavaScript. See [Run untrusted code](/files-and-code/run-untrusted-code) |
| `handlers` | Its handlers |

`defineLuna` refuses a stateless type with a key, tables, streams, buckets, sandboxes, hooks, crons, listeners or pushes. A deploy refuses a stateless handler marked `query`, `shared`, `exclusive`, `durable` or `requires`.

## Use the stateless context

A stateless handler's `ctx` is a [`StatelessCtx`](/sdk/ctx). It has no `ctx.key`, `ctx.db`, `ctx.sql`, `ctx.transaction`, `ctx.send`, `ctx.schedule`, `ctx.emit` or `ctx.step`.

| Member | Does |
| --- | --- |
| `ctx.call` | Calls a keyed luna, or another stateless type with the key `{}` |
| `ctx.env` | The environment's secrets |
| `ctx.isolates` | Runs JavaScript in an isolate |
| `ctx.tenants`, `ctx.services` | Calls lunas in other tenants. See [Call across tenants](/platforms/call-across-tenants) |
| `ctx.fail` | Fails the call with a declared error |
| `ctx.now()`, `ctx.random()`, `ctx.uuid()` | The time, a random number and a new UUID |
| `ctx.caller`, `ctx.request` | Who called, and the HTTP request for a call through a route |
| `ctx.deadline`, `ctx.abortSignal` | When the call must answer by, and a signal that fires when the caller goes away |
| `ctx.callId`, `ctx.traceId`, `ctx.version` | The call's id, its trace and the version of the code it runs |

Anything a call needs to remember goes to a keyed luna with `ctx.call`. Memory left from one call may be gone in the next.

## Choose stateless or keyed

| Use a stateless luna type for | Use a keyed luna type for |
| --- | --- |
| Work that keeps nothing between calls | Anything that owns data, such as a list, a user or a room |
| An API in front of many lunas | Calls that must take turns on the same data |
| Checks and transforms that need many calls at once | Schedules, durable runs, events and pushes |

Rule of thumb: if the work has to remember anything, it belongs in a keyed luna.

Stateless lunas can be written in Python too. See [Write lunas in Python](/lunas/write-lunas-in-python).

## Next steps

- [Call another luna](/lunas/call-another-luna) covers timeouts and idempotency keys for `ctx.call`.
- [Serve HTTP routes](/http/serve-http-routes) puts a stateless handler behind a path of your own.
- [defineLuna](/sdk/define-luna) lists every option of a stateless type.
