> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Submit external effects

> Declare a target, submit a task in the business transaction and receive its result in a continuation.

A target is a contract for work done outside the transaction: what it
takes (`args`), what it gives back (`returns`), the app codes it may refuse
with (`refuses`), how long it may take (`within`), and who carries it out
(`via`, its performer). `ctx.submit` commits the intent atomically with the
mutation's writes. The performer runs after the commit, and the task
settles once, with a `Result`.

## Declare a target over HTTP

`http(url, { bearer })` performs a task as one JSON `POST` of its args. A
2xx answer's JSON body is the value. A 4xx answer whose body names one of
the target's `refuses` codes settles with that code. An answer that never
arrived settles `Unknown`.

```ts bijection/payments.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { http, label, secret, t, target } from "@bijectionhq/bijection";

/** Set it once: `bijection secret set payments.key`. */
export const key = secret();

export const charge = target({
  label: label.public,
  via: http("https://payments.example.com/charges", { bearer: key }),
  args: { order: t.string, amount: t.decimal, currency: t.string },
  returns: { id: t.string },
  refuses: { CardDeclined: null },
  within: { minutes: 10 },
});
```

For an API whose requests need a method, a path, headers or a keyed
retry contract, declare a `receiver({ endpoint, auth, contract, operations })`
from `@bijectionhq/bijection`. Each `operation({ method, path, args,
returns, request, answer })` is a target, and the receiver's `contract`
states what the API promises about a repeated request (`once` or `keyed`).
The [controlled HTTP example](https://github.com/bijectionhq/bijection/tree/main/examples/controlled-http)
declares one.

## Submit and continue

```ts bijection/orders.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { mutation, role, t } from "@bijectionhq/bijection";
import { charge } from "./payments";
import { orders } from "./schema";

export const charged = mutation(
  { args: { order: t.key(orders), result: t.result(charge) } },
  async (ctx, { order, result }) => {
    if (result.ok) await ctx.patch(orders(order), { state: "paid", charge: result.value.id });
    else if (result.code === "Unknown") await ctx.patch(orders(order), { state: "unsure" });
    else await ctx.patch(orders(order), { state: "declined", reason: result.message });
  },
);

export const pay = mutation(
  { allow: [role.signedIn], args: { order: t.key(orders) }, refuses: { NotPlaced: null } },
  async (ctx, { order }) => {
    const fact = await ctx.get(orders(order));
    if (fact === null || fact.state !== "placed") return ctx.refuse("NotPlaced", "Place the order first.");
    const task = await ctx.submit(
      charge,
      { order, amount: fact.total, currency: fact.currency },
      { once: ["charge", order], then: charged.with({ order }) },
    );
    await ctx.patch(orders(order), { state: "charging", task });
  },
);
```

* `once` is the effect's identity. Submitting again with the same `once`
  and args returns the original task; the same `once` with other args
  refuses `Invalid`.
* `then` names the continuation, a mutation with its other args bound by
  `fn.with(args)`. The delivery fills `result`. The continuation is
  internal: it declares no `allow`.
* `due` holds the task until an instant; `within` overrides the target's
  deadline. Past the deadline, a pending task settles `Expired`.

The continuation is decided afresh, in its own transaction, through the
submitter's grants recorded at submit that are still live. A revoked grant
refuses the step.

## Performed by people

A target whose performer is a list of roles waits for a principal holding
one of them to settle it:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
export const signOff = target({
  via: [manager],
  args: { order: t.key(orders) },
  returns: { approved: t.boolean },
  within: { hours: 72 },
});

export const decide = mutation(
  { allow: [manager], args: { task: t.task(signOff), approved: t.boolean } },
  (ctx, { task, approved }) => ctx.answer(task, { ok: true, value: { approved } }),
);
```

`ctx.answer(task, result)` settles a pending task, and `ctx.withdraw(task, { message })`
settles it `Withdrawn`. Both are grave acts: `bijection check` lists the
functions that perform them. A role-performed target always declares
`within`, since a person may never answer. For structured, attributed
approvals, use the [Approvals app](https://docs.bijection.com/operations/review.md).

## Performed by a mutation

`via: mutation` makes a mutation the performer. The host runs it in its
own transaction with the task's args, and its value (or refusal) settles
the task. A performer that needs to wait returns its own task,
`ctx.frame.on.task`. Declare the mutations that may answer it with
`target({ via: perform, answeredBy: [review] })`. An answerer calls `ctx.answer`. A mutation is also a target in its own right:
`ctx.submit(recount, { day }, { due })` runs it later.

## Lost replies

`Unknown` means the work may have happened. The host never performs the
task again. Keep the receiver's own identity for the command and ask the
receiver what happened, as [Reconciliation](https://docs.bijection.com/operations/reconciliation.md)
describes, before you submit a second business effect.

The [outbound webhook example](https://github.com/bijectionhq/bijection/tree/main/examples/webhooks-outbound)
shows a continuation that records a mounted app's delivery result.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.