# SDK quickstart

Call RecruitAffiliates.ai from TypeScript or JavaScript with @recruitaffiliates/sdk: install, a first search, emails, errors and retries.

An **SDK** is a code library that makes the web requests for you. You write `ra.searches.start({ ... })`, and your editor knows every field. `@recruitaffiliates/sdk` covers every operation in the [API](/api). It has types for what you send and what comes back.

<Callout type="warn">
Use it on a server only. Your API key can do everything your account can. A key in a browser bundle, a mobile app or a public repository is a key anyone can read.
</Callout>

## Install

```bash
npm install @recruitaffiliates/sdk
```

It needs Node 18 or later, works with `import` and `require`, and carries its own types.

## Get a key

Open **Settings** in the app and go to the API tab. Make a test key while you build: it starts with `rail_test_`, only sees sample data and never spends anything. [Get a key](/api#get-a-key) has the details. Keep the key in an environment variable, never in your code.

## A first run

```ts
import { RecruitAffiliates } from "@recruitaffiliates/sdk";

const ra = new RecruitAffiliates({ apiKey: process.env.RA_API_KEY! });

// Start a search. On a live key this uses one search from your plan.
const search = await ra.searches.start({ type: "topic", seed: "trail running shoes" });

// Wait for it. A first search takes 10 to 25 minutes.
const done = await ra.searches.wait(search.id, {
  onProgress: (s) => console.log(`${s.found} matches so far`),
});
console.log(done.state); // "done", "failed" or "stopped_at_ceiling"

// Read your 10 best matches, save them, and look for their emails.
const top = await ra.affiliates.list({ sort: "fit", limit: 10 });
const ids = top.data.map((a) => a.id);
await ra.affiliates.save({ ids });
for (const id of ids) {
  const found = await ra.affiliates.revealEmail(id); // one email lookup, only when an address comes back
  console.log(id, found.email ?? "no email found");
}
```

Every answer says what it is in its `object` field, such as `recruit_search` or `recruit_prospect`.

## Every page of a list

`list` gives one page. `listAll` gives every item and only asks for the next page when your loop gets there:

```ts
for await (const affiliate of ra.affiliates.listAll({ fit: "strong" })) {
  console.log(affiliate.name);
}
```

## Errors

Any answer that isn't a success throws `RecruitAffiliatesError`. Branch on `code`, never on the message. `fix` says what to do in one sentence, and `requestId` is the id to quote if you ask us for help.

```ts
import { RecruitAffiliatesError } from "@recruitaffiliates/sdk";

try {
  await ra.campaigns.pause("fca_123");
} catch (error) {
  if (error instanceof RecruitAffiliatesError && error.code === "resource_missing") {
    console.log("No such campaign.", error.requestId);
  } else throw error;
}
```

## What it does for you

- **A retry never repeats a write.** Each write carries an **idempotency key**: a unique string that tells the server "this is the same request as before". If a write arrives twice, it runs once. So a retry can't start a second search or spend a second lookup.
- **Retries.** After a `429` it waits for the seconds in `Retry-After`. It also tries again after a `500`, `502`, `503` or `504` and after a dropped connection, twice by default. It never retries a delete that may already have run.
- **Your key stays out of logs.** It isn't in any error, and `console.log(ra)` shows nothing secret.

## Sending email

A campaign launch, an approval, a reply or an invite needs a key that can send email (`finder:send`). Each one answers with a card first: what will go out and to whom. Show it to the founder, then call again with the card's `preview_token`. Once confirmed, a launch waits 10 minutes before anything leaves and a reply waits 2, and `ra.agentActions.undo` takes it back inside that time.

## Every method

Methods are named `ra.<area>.<action>`, and the [package README](https://www.npmjs.com/package/@recruitaffiliates/sdk) lists them all. The area is the API's resource name, except prospects, which the app calls affiliates: `GET /prospects` is `ra.affiliates.list`. For a call the SDK doesn't name, `ra.request("GET", "/path", { query, body })` adds the key, the idempotency key and the retries for you.

Rather work from a terminal? The [CLI](/cli) does the same from the command line.
