Bulk import

A CSV to passes

Markdown

Turn a spreadsheet into passes in three calls: create the import, check it, commit it. Check reports every row that would fail before anything is made. A sandbox makes test previews only.

Create the import

The file is sent as text. Map its columns in the next step.

{  "templateId": "0193a1b2-7c00-7000-8000-0000000000b1",  "source": {    "kind": "file",    "name": "guests.csv",    "csv": "attendee,ticket,email\nAda Example,TICKET-1001,ada@example.com\nGrace Host,TICKET-1002,grace@example.com\nAlan Guest,TICKET-1003,alan@example.com\nEdsger Visitor,TICKET-1004,edsger@example.com\nBarbara Member,TICKET-1005,barbara@example.com"  }}
Open in consoleOpens the console with this request filled in.

Keep the import id.

Check the rows

Map each template field to a column or a constant. Paste the import id into the path.

{  "plan": {    "mapping": {      "attendee": { "from": "attendee" },      "ticketId": { "from": "ticket" },      "eventName": { "from": "attendee", "transform": { "kind": "constant", "options": { "value": "Harbor Night" } } },      "venueName": { "from": "attendee", "transform": { "kind": "constant", "options": { "value": "Harbor Hall" } } },      "venueRegion": { "from": "attendee", "transform": { "kind": "constant", "options": { "value": "Cupertino, CA" } } },      "startsAt": { "from": "attendee", "transform": { "kind": "constant", "options": { "value": "2030-06-01T19:00:00-07:00" } } },      "endsAt": { "from": "attendee", "transform": { "kind": "constant", "options": { "value": "2030-06-01T22:00:00-07:00" } } }    },    "holderEmail": { "from": "email" },    "externalId": { "from": "ticket" },    "match": "externalId"  }}
Open in consoleOpens the console with this request filled in.

Bad rows come back with the reason. Fix the sheet or commit with skipInvalid.

Commit

{ "skipInvalid": true }
Open in consoleOpens the console with this request filled in.

Commit runs in the background. Read the import until state is done. One import of a template commits at a time: a second commit meanwhile is 409 IMPORT_BUSY.

In code

Import passes from a CSV
import { PassBook } from '@passbook/sdk';const pb = new PassBook({ apiKey: process.env.PASSBOOK_KEY ?? '' });const csv = [  'attendee,ticket,email',  'Ada Example,TICKET-1001,ada@example.com',  'Grace Host,TICKET-1002,grace@example.com',].join('\n');const draft = await pb.passImports.create({  templateId: '0193a1b2-7c00-7000-8000-0000000000b1',  source: { kind: 'file', name: 'guests.csv', csv },});// Check reports every row that would fail, before anything is made.const checked = await pb.passImports.check(draft.id, {  plan: {    mapping: {      attendee: { from: 'attendee' },      ticketId: { from: 'ticket' },      eventName: { from: 'attendee', transform: { kind: 'constant', options: { value: 'Harbor Night' } } },      venueName: { from: 'attendee', transform: { kind: 'constant', options: { value: 'Harbor Hall' } } },      venueRegion: { from: 'attendee', transform: { kind: 'constant', options: { value: 'Cupertino, CA' } } },      startsAt: { from: 'attendee', transform: { kind: 'constant', options: { value: '2030-06-01T19:00:00-07:00' } } },      endsAt: { from: 'attendee', transform: { kind: 'constant', options: { value: '2030-06-01T22:00:00-07:00' } } },    },    holderEmail: { from: 'email' },    externalId: { from: 'ticket' },    match: 'externalId',  },});console.log(checked);// Commit runs in the background. Read the import again to follow it.await pb.passImports.commit(draft.id, { skipInvalid: true });const status = await pb.passImports.retrieve(draft.id);console.log(status.state);

Act on up to 100 passes at once

Void, email or remove passes by id.

Void many passes
curl -X POST "https://passbook.mobile/api/v1/passes/bulk" \  -H "Authorization: Bearer $PASSBOOK_KEY" \  -H "Content-Type: application/json" \  -d '{  "action": "void",  "ids": [    "0193a1b2-7c00-7000-8000-0000000000c1"  ]}'
import { PassBook } from '@passbook/sdk';const pb = new PassBook({ apiKey: process.env.PASSBOOK_KEY ?? '' });await pb.passes.bulkApply({ action: 'void', ids: ['0193a1b2-7c00-7000-8000-0000000000c1'] });
Copy as
Open in consoleOpens the console with this request filled in.

Result

Read the import: state is committing while it runs and done after, with the counts. Then list the passes; each has a downloadUrl to its test preview. When it is done, results.csv and errors.csv list what each row did.

Operations