# Set up a project

Create a client, a project under it and a consultant's assignment through the TimeSubmit API, with an Idempotency-Key on each write so retries are safe.

When a new piece of work is agreed in your CRM, you can set it up in TimeSubmit in three calls: create the client, create the project under it, then assign a consultant to it. The consultant can then log time against the project. Each call is a write, so each sends an `Idempotency-Key` and can be retried safely.

The key needs the `clients:write`, `projects:write` and `assignments:write` scopes, and `consultants:read` to find the consultant. Add the read scopes too if you want each write to answer with the whole record. See [authentication](https://www.timesubmit.com/developers/authentication).

## Send each write once

Make a new `Idempotency-Key` for each change, store it with the change in your own system, and send the same key if you retry. Within 24 hours a retry gets the first answer back and changes nothing, so a timeout never creates a second client. The [idempotency](https://www.timesubmit.com/developers/idempotency) page has the rules.

The examples use this small helper:

```js
import { randomUUID } from "node:crypto";

const BASE = "https://www.timesubmit.com/api/v1";

async function timesubmit(method, path, body, idempotencyKey = randomUUID()) {
  const response = await fetch(`${BASE}${path}`, {
    method,
    headers: {
      Authorization: `Bearer ${process.env.TIMESUBMIT_API_KEY}`,
      "Content-Type": "application/json",
      ...(method === "GET" ? {} : { "Idempotency-Key": idempotencyKey }),
    },
    body: body === undefined ? undefined : JSON.stringify(body),
  });
  const answer = await response.json();
  if (!response.ok) throw new Error(`${answer.error.code}: ${answer.error.message}`);
  return answer;
}
```

In production, keep the key you generated so a retry after a crash reuses it.

## 1. Create the client

```js
const client = await timesubmit("POST", "/clients", {
  name: "Northwind Traders",
  contactName: "Priya Shah",
  contactEmail: "priya.shah@northwind.example",
  currency: "GBP",
  paymentTermDays: 30,
});
```

The answer is the new client, with its `id`. If the client already exists in TimeSubmit, skip this step and use its id: `GET /clients` lists them. A plan at its client limit is refused with `client_limit_reached`.

## 2. Create the project

```js
const project = await timesubmit("POST", "/projects", {
  clientId: client.id,
  name: "Warehouse migration",
  startDate: "2027-01-04",
  hourlyRate: 95,
  currency: "GBP",
});
```

A new project starts in planning. When the work begins, set its status with `PATCH /projects/{id}`. The [projects reference](https://www.timesubmit.com/developers/reference/projects) lists every field and status.

## 3. Assign a consultant

Consultants join your organisation through the dashboard, not the API, so find the one you want with `GET /consultants` and use their id.

```js
const assignment = await timesubmit("POST", "/assignments", {
  projectId: project.id,
  consultantId: "q2w7e1r6t3y8u4i9o5p0a1s2d",
  rate: 650,
  rateType: "daily",
  costRate: 500,
  costRateType: "daily",
  currency: "GBP",
  startDate: "2027-01-04",
});
```

`rate` is what the client is charged and `costRate` is what the consultant is paid. A consultant can be assigned to a project once: a second attempt is refused with `already_exists`, so a retry with a new key after the first succeeded tells you the assignment is already there.

## When something is refused

Each refusal names its code. The ones this flow meets most are `invalid_request` (the message names the field), `client_limit_reached` or `project_limit_reached` (your plan's limit), and `read_only` (your organisation can't take writes just now). The [errors](https://www.timesubmit.com/developers/errors) page lists every code and what to do about it.
