> For the complete documentation index, see [llms.txt](https://docs.trustlesswork.com/trustless-work/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.trustlesswork.com/trustless-work/v2-en/escrow-js-sdk/getting-started.md).

# Getting Started

Install @trustless-work/escrow-js and create a TrustlessWorkClient.

### Overview

The Trustless Work JavaScript SDK is a typed, framework-agnostic client over Core API v2. It uses **native `fetch`** (Node 18+ / browsers) — no Axios, no React peers.

{% hint style="info" %}
Write flows typically need an API key. Optional wallet-session auth uses `getAccessToken` (sync or async).
{% endhint %}

### Quick links

<table data-view="cards"><thead><tr><th>Title</th><th data-card-target data-type="content-ref">Link</th></tr></thead><tbody><tr><td>NPM package</td><td><a href="https://www.npmjs.com/package/@trustless-work/escrow-js">https://www.npmjs.com/package/@trustless-work/escrow-js</a></td></tr><tr><td>Architecture</td><td></td></tr><tr><td>Send signed XDR</td><td></td></tr><tr><td>Runtimes</td><td></td></tr></tbody></table>

### Setup

{% stepper %}
{% step %}

### Installation

{% tabs %}
{% tab title="npm" %}

```sh
npm i @trustless-work/escrow-js@beta
```

{% endtab %}
{% endtabs %}

Requires **Node.js ≥ 18** (global `fetch`). Zero runtime dependencies.

{% hint style="info" %}
Install under the `beta` dist-tag while Core v2 is in beta, so `latest` can stay on the audited Core v1 line for other packages.
{% endhint %}
{% endstep %}

{% step %}

### Create a client (recommended)

```ts
import { TrustlessWorkClient, development } from "@trustless-work/escrow-js";

const client = new TrustlessWorkClient({
  baseURL: development,
  apiKey: process.env.API_KEY,
});

await client.rest.listEscrows({ scope: "mine", limit: 20 });
await client.graphql.getEscrow({ contractId: "C..." });
```

This is the preferred pattern for **NestJS / Angular DI** and server apps.
{% endstep %}

{% step %}

### Or configure a default client

Vanilla equivalent of React’s `TrustlessWorkConfig`:

```ts
import {
  configureTrustlessWork,
  escrowRest,
  escrowGraphql,
  development,
} from "@trustless-work/escrow-js";

configureTrustlessWork({
  baseURL: development,
  apiKey: process.env.API_KEY,
  getAccessToken: async () => sessionToken,
  defaultHeaders: { "X-TW-Platform": platformId },
});

await escrowRest().listEscrows({ scope: "mine", limit: 20 });
await escrowGraphql().getEscrow({ contractId: "C..." });
```

{% endstep %}

{% step %}

### Build → sign → send

Typical **operate** flow:

1. Call a REST method to get an **unsigned XDR** (and for deploy, a predicted `contractId`).
2. Sign the XDR with the correct role wallet.
3. Submit with `sendTransaction`.

For **reads**, call REST or GraphQL methods directly — they return Promises (pair with your own cache if needed).
{% endstep %}
{% endstepper %}

### Client options

| Option           | Role                                                                 |
| ---------------- | -------------------------------------------------------------------- |
| `baseURL`        | Core API host (`development` / `mainNet`, or any URL string)         |
| `apiKey`         | `x-api-key` header                                                   |
| `getAccessToken` | Optional Bearer getter — sync **or** `Promise` (re-read per request) |
| `defaultHeaders` | Merged into every request (e.g. `X-TW-Platform`)                     |
| `fetch`          | Optional custom fetch (tests, polyfills, undici)                     |
| `timeoutMs`      | Optional timeout via `AbortSignal.timeout`                           |

### Next steps

* Architecture — entry points and layers
* Operate — mutate methods
* REST Reads — `GET /escrows*`
* Runtimes — Node, NestJS, Angular, browser
* Errors — Problem Details helpers

{% hint style="success" %}
Supports **single-release** and **multi-release**. Use the matching payload type and `EscrowType` for each flow.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.trustlesswork.com/trustless-work/v2-en/escrow-js-sdk/getting-started.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
