> ## 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.

# AS2 and AS4 trading partners

> Mount a signed, encrypted document exchange on your app.

Install one mount per trading agreement. The app submits documents and retains
signed receipts in its own tables. For inbound AS4, an external gateway verifies
the partner and the app commits the document, message position and exact signed
receipt together. The `data` label you pass governs the documents, receipts,
identity records and task results.

## Mount one agreement

`as2({ data, partner })` returns the app to place at a path of your tree.
Bind the partner endpoint with `as2Partner(origin, path, credential, label)`.

```ts app.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, label, secret } from "@bijectionhq/bijection";
import as2, { as2Partner } from "@bijectionhq/app-as2";

const edi = label();
const outbound = secret();
const partner = as2Partner("https://edi.acme.example", "/as2", outbound, edi);

export default app({ edi, outbound, partner, acme: as2({ data: edi, partner }) });
```

For AS4 use `@bijectionhq/app-as4`, `as4({ data, partner })` and
`as4Partner`. The mount places the catalog's trading-partner subtree at
`acme.partner` (`partner.send`, `partner.get` and `partner.list`, with its
`outgoing` and `incoming` tables) and declares its `operator` role, which
clears `data`, may call those, and runs the dispatch clock.

Set the mount's outbound credential from the agreement JSON.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection secret set outbound --from-file outbound.json
```

The AS2 mount declares its own inbound `agreement` secret.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection secret set acme.agreement --from-file agreement.json
```

`acme.operator` is granted from code: `ctx.grant(acme.operator, { to, under })` in one of your
mutations ([Grants](https://docs.bijection.com/access/grants.md)).

For a protected partnership, pass the same declared label as `data` and as
the last argument to `as2Partner` or `as4Partner`.

The sealed agreement is JSON with the local PKCS#8 PEM signing and decryption key
(`our_key`), its certificate (`our_certificate`), the pinned partner PEM
certificate (`partner_certificate`), and an optional separate pinned
`partner_encryption_certificate`. Set `partner_url` to the exact HTTPS
endpoint the receiver was bound with. Choose the `signing` and
`encryption` algorithms agreed with the partner. The outbound endpoint uses
the host's HTTPS trust and network policy.

* AS2 also requires `as2_id`, `partner_as2_id` and `mdn: "synchronous"`.
  Inbound AS2 requires a synchronous signed SHA-256 MDN request.
* AS4 also requires `party_id`, `partner_party_id`, `service` and
  `action`. Set the optional `agreement`, party ID types and message
  properties according to the partnership. The supported exchange carries
  one signed and encrypted attachment.

## Receive AS4 through the gateway

Use a private `data` label and a dedicated signing key of at least 32 bytes
for the mount. This command registers the key for the `acme` mount's existing
`POST /receive` route.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection secret set acme.signing --app /acme --route 'POST /receive' \
  --from-file /path/to/ingress-key
```

Set `BIJECTION_AS4_INGRESS_KEY` to that same key on the gateway. Set
`BIJECTION_AS4_AGREEMENT` to the inbound agreement JSON, including the
gateway's private key and the partner certificate pin. The gateway's `--route`
takes the mount's site URL.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection-provider-gateways --listen 0.0.0.0:3228 \
  --tls-cert /path/to/gateway-cert.pem --tls-key /path/to/gateway-key.pem \
  as4-source --route https://site.example/acme/receive
```

The partner POSTs its AS4 envelope to `/receive` on the gateway. The app sees
the decoded document, signed receipt and authenticated message position over
the mount's timestamped HMAC route. The gateway answers with the exact committed
SOAP receipt, including on a retry of the same message and collaboration. A
different document or collaboration at that position is refused.

## Publish, inspect and reconcile

As the operator, call `acme.partner.send` with a stable application `key`,
canonical base64 `data`, `contentType` and `fileName`. Repeating the same key and document returns the original row.
The mount's ten-second clock submits one pending document at a time. Its send
acceptance clears the queued payload. Use `partner.get` for state and receipt. A confirmed AS2 MDN matches
the Message-ID and MIC. A confirmed AS4 receipt matches the MessageId and
signed references. Acceptance means the trading transport acknowledged the
document. A lost or inconclusive response settles the task `Unknown`, for the
operator to reconcile with the partner.

Identical authenticated deliveries converge on one inbound File and row.
Conflicting use of a retained identity is refused. Call `partner.list` as the operator to inspect the retained deliveries. A peer may
retry a lost inbound response. The app recognizes the retained delivery and
the route returns a signed receipt.

Each document is bounded to 24 KiB and each HTTP request to 128 KiB. This
source implementation has yet to be compiled or tested.


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