# The door

> Check-in, offline scanning, look-up and gate sales, with a phone or with your own scanner

Source: https://tickets.roostertail.com/docs/door

A ticket's QR code is signed. A scan checks the signature, the ticket's status, its version and the day, and answers what to do. We ship a door app at `/scan`, and everything it does is in the API, so you can scan from your own app or hardware.

## Who can scan

- **A door device.** A phone an organizer authorizes for one event. It pairs once with a one-time link and then holds its own token (`rtd_…`), which can scan and, if allowed, sell. It cannot read anything else.
- **A `checkin` key.** A server key that can scan and look up, and sees guests' names in short form ("Maria D.").
- **A full key**, or a signed-in member whose role may check in.

A phone holds a device token, never an API key.

## Scan

Send the QR code's text as `token`. The answer is always `201`, even for a refusal: read `result`.

A scan counts for the day on at the venue now: from its doors to 3 hours after its end. A scan before doors on that day's date counts for that day too. The sample makes a small event that is open now, sells it a ticket and scans it. (The event from the quickstart is weeks away, so scanning one of its tickets today answers `wrong_day`.)

```ts
const venue = {
  name: 'Riverfront Stage',
  address: { line1: '100 Example Ave', line2: null, city: 'Detroit', region: 'MI', postal_code: '48201', country: 'US' },
  timezone: 'America/Detroit',
  location: null,
};
const hour = 3600 * 1000;
const doorsAt = new Date(Date.now() - hour);
// The day the doors open at the venue: just after midnight, that's still yesterday.
const localDate = new Intl.DateTimeFormat('en-CA', { timeZone: venue.timezone }).format(doorsAt);

const open = await tickets.bulk.setup({
  event: {
    name: 'Tonight at the Stage',
    currency: 'usd',
    venue,
    days: [{ date: localDate, name: 'Tonight', doors_at: doorsAt.toISOString(), starts_at: new Date(Date.now() - hour / 2).toISOString(), ends_at: new Date(Date.now() + 3 * hour).toISOString() }],
  },
  ticket_types: [{ ref: 'door', name: 'Door', price: { amount: 3000, currency: 'usd' }, capacity: 50 }],
});
const tonight = open.event!.id;
await tickets.events.publish(tonight);

const heldTonight = await tickets.holds.create({ event: tonight, items: [{ ticket_type: open.ticket_types[0]!.id!, quantity: 1 }] });
const boughtTonight = await tickets.orders.create({ hold: heldTonight.id, buyer: { name: 'Maria Delgado', email: 'maria@example.com' }, payment: { provider: 'test', token: 'tok_success' } });
const entry = (await tickets.orders.retrieve(boughtTonight.id)).tickets[0]!;

const admitted = await tickets.checkins.create({ token: entry.qr_token!, event: tonight });
console.log(admitted.result); // admitted

const again = await tickets.checkins.create({ token: entry.qr_token!, event: tonight });
console.log(again.result, again.previous !== null); // already_in true

const early = await tickets.checkins.create({ token: (await tickets.orders.retrieve(order.id)).tickets[0]!.qr_token!, event });
console.log(early.result); // wrong_day
```

| `result` | Say |
| --- | --- |
| `admitted` | Let them in. `reentry` is true when the event allows coming back the same day. |
| `already_in` | `previous` says when and where the first admission was. |
| `wrong_day` | The ticket is good for another day. |
| `not_valid` | `reason`: `void`, `transferred` (an older QR after a transfer), `reissued`, `bad_signature`, `unknown_ticket`, `event_cancelled` or `device_revoked`. |
| `frozen` | A dispute froze it: send them to the box office. |
| `wrong_event` | A ticket for another event. |
| `refund_pending` | A refund on its way will void it. Not admitted: send them to the box office. |

You may send `day`, but only the day that is on now: any other day is a 400, so a script can never use up Sunday's admission on Friday. Leave it out and the clock chooses. A ticket with a minimum age is admitted with the answer "Check ID" until the person at the door says they looked at it: `id_checked: true` on the scan, or `tickets.checkins.idOk(id)` after it.

An admission can be undone for 60 seconds (`tickets.checkins.undo`). After that a manager takes it back with a reason (`correct`), and the ticket can be scanned again.

## No QR code

`POST /v1/checkins/lookup` finds a ticket by a first or last name, email, phone or order number. The query is in the body, never the URL. Then scan by `ticket: 'tkt_…'` instead of `token`. Every manual look-up is recorded as `method: manual`.

A name needs 3 letters from the start of any word (`Delg`, `Maria`, `Smith-Jones` by `Jones`), or a whole 2-letter name (`Li`). Case and accents don't matter. Several words must each match, in any order. It answers at most 10 tickets, and `has_more` says when others match: add the first name.

## Watching the door

`GET /v1/events/{id}/checkin_stats` has the counts: admitted and still expected, by day, gate and ticket type, with the arrival curve. Poll it. `GET /v1/checkins` lists scans, but a door member or a `checkin` key sees only the last hour of one event, and it counts against a daily budget, so it is not a monitor.

```ts
const stats = await tickets.events.checkinStats(event);
console.log(stats.object); // checkin_stats
```

## Offline

A door phone downloads everything it needs with `GET /v1/events/{id}/checkin_manifest` (tickets, public keys and days; `since` asks for changes only), checks QR signatures with no network, and sends what it scanned with `POST /v1/checkins/sync` when it is back online. Each offline scan answers with the server's own result.

If two phones admitted the same ticket while offline, the server's first scan wins and the other becomes a **conflict** (`checkin_conflict.created`), which a manager resolves as `kept_first` or `dismissed` (`checkin_conflict.resolved`). Money or the order moving on a guest already in today is one too: a refund (`refund_after_admission`), an order cancelled (`cancel_after_admission`) or a lost dispute (`dispute_after_admission`). They are inside, with the money back or on a ticket that no longer works.

A revoked phone can still send its offline scans, for 24 hours from the revoke, and do nothing else. So can a phone whose own `expires_at` came before the event ended. It gets a receipt, not an answer. A scan dated before the revoke counts. A later one is a conflict, never an admission. After that the phone says how many scans didn't send, then empties itself.

It may hand in 2,000 records at most, whatever they are. Each is marked `handed_in`, and `Device.hand_in_count` counts them. `POST /v1/devices/{id}/hand_in` deals with them: `take_back` corrects every admission it handed in with one reason, `keep` lets them stand, `end_window` stops its sync now.

With no signal a phone can't know about a refund: it knows only what its last list said, and the list doesn't carry refunds. The sync finds out, and makes a conflict.

## Authorize a phone

```ts
const device = await tickets.devices.create({ event, name: 'Front gate', gate: 'Front', sales_allowed: true });
console.log(device.status, device.pairing_url !== null); // pending true
```

The answer holds a one-time `pairing_url`: open it on the phone, or scan its QR code. No answer is kept for a retry, because it holds the pairing code. If the answer of a create was lost, list the devices before you try again. A phone that never paired does nothing and can be revoked (`tickets.devices.revoke`), after which its next call answers `device_token_invalid`, but for sending its offline scans (above).

## Selling at the gate

A device that is allowed to sell (`sales_allowed`) makes a hold, as any checkout does, and then a **door sale** from it. The hold's price is the total to take.

- `POST /v1/door_sales` takes `{ hold, payment, buyer?, admit?, id_checked? }`. **It requires an `Idempotency-Key`** and answers 400 `parameter_missing` without one, so a retry on bad wifi never sells twice.
- The payment is `{ method: 'cash', amount, tendered? }`, `{ method: 'card_reader', amount, reference }` or, in test mode, `{ method: 'test', token }`. Cash and a reader are recorded as `external` payments: the money was taken at the gate. The amount must equal the hold's total.
- The tickets are admitted at once unless `admit` is false, and only if they are valid today.
- A ticket with a minimum age is sold but not admitted unless `id_checked: true`.
- A buyer is optional. An email gets the tickets by email. Only a phone gets them by text, where texts are on and the org allows the number's country, and the number stays on the order. This is a service message, like a comp's: no tick is needed. `buyer.marketing` is only about future shows. `delivery` on the sale says what we'll try to send, and `delivery.not_sent_why` why not.
- `POST /v1/close_outs` closes a device's cash and card at the end of a shift. `dry_run: true` shows the totals so far.

A door sale is a sale like any other: it counts in reports and refunds the way an order does.
