---
title: Call another luna
description: Call another luna and wait for its answer, send it a message that is delivered later, and list the lunas of a type.
---

A luna owns only its own data, so work that spans two lunas needs one to ask the other. Call a luna when you need its answer, and send it a message when you do not.

## Call and wait for the answer

`ctx.call` takes a luna type and a key, and returns its handlers. Await one to get its result.

```ts luna/lunas/shop.ts
import { defineLuna, t } from "luna";
import { Wallet } from "./wallet.ts";

export const Shop = defineLuna({
  name: "Shop",
  key: { id: t.string() },
  handlers: (h) => ({
    buy: h({ args: { amount: t.int() } }, async (ctx, { amount }) => {
      const left = await ctx.call(Wallet, { id: ctx.key.id }).charge({ amount });
      return { left };
    }),
  }),
});
```

The writes your handler made before the call are stored before the call is sent. Calls between lunas skip `onBeforeConnect`, and private luna types can be called.

A call that succeeded stays done. If your handler fails afterwards, its own writes are undone, but the other luna keeps its change.

## Set a timeout and retry safely

A call waits up to 60 seconds, and never past your own handler's deadline. Pass a `timeout` to change that, and an `idempotencyKey` so that a retried call runs once.

```ts luna/lunas/shop.ts
await ctx.call(Wallet, { id: ctx.key.id }).charge(
  { amount },
  { timeout: "5s", idempotencyKey: `order-${orderId}` },
);
```

A retry with the same key returns the first call's result, without running the handler again.

## Send a message

`ctx.send` delivers a call later, and your handler does not wait for it. It returns the message's id.

```ts luna/lunas/shop.ts
refund: h({ args: { amount: t.int() } }, (ctx, { amount }) => {
  ctx.send(Wallet, { id: ctx.key.id }).credit({ amount });
}),
```

A message is sent only if your handler succeeds. It is stored with your writes, so it is never lost and never sent for a call that failed.

Messages from one luna to another arrive in the order they were sent. A message that fails is tried again, up to 10 times in all over about a day.

| Option | Does |
| --- | --- |
| `delay` | Waits before delivering, such as `"10m"` |
| `maxAttempts` | How many times to try, 10 by default |
| `firstRetry` | How long to wait before the first retry |
| `dedupeKey` | Delivers once, however many messages share this key |

```ts luna/lunas/shop.ts
ctx.send(Wallet, { id: ctx.key.id }).credit({ amount }, { delay: "10m", dedupeKey: `refund-${orderId}` });
```

## Know who called

In the luna that was called, `ctx.caller` names the luna that made the call or sent the message.

```ts luna/lunas/wallet.ts
charge: h({ args: { amount: t.int() }, errors: { Denied: t.json() } }, (ctx, { amount }) => {
  if (!("luna" in ctx.caller)) ctx.fail("Denied");
  // ...
}),
```

## List the lunas of a type

`ctx.list` returns the keys of lunas that exist, in key order, a page at a time.

```ts luna/lunas/admin.ts
let page = await ctx.list(List, { limit: 100 });
for (;;) {
  for (const key of page.keys) ctx.send(List, key).tidy({});
  if (!page.cursor) break;
  page = await ctx.list(List, { cursor: page.cursor, limit: 100 });
}
```

Pass `prefix` to list only keys that start with some key fields, such as every `Card` of one board.

## Call a luna type by name

A luna type written in another language, such as Python, is called by its name. `luna dev` writes its type to `luna_gen.ts`.

```ts luna/lunas/shop.ts
import type { Wallet } from "../luna_gen.ts";

await ctx.call<Wallet>("Wallet", { id: ctx.key.id }).charge({ amount });
```

## Next steps

- [Return errors](/lunas/return-errors) shows what a failed call throws.
- [The life of a luna](/concepts/the-life-of-a-luna) explains how calls to one luna take turns.
