iOS Passbook for the Node hacker
To start with, you'll need a certificate issued by the iOS Provisioning Portal. You need one certificate per Passbook Type ID.
After adding this certificate to your Keychain, you need to export it as a
.p12 file and copy it into the keys directory.
You will also need the 'Apple Worldwide Developer Relations Certification
Authority' certificate and to conver the
.p12 files into
.pem files. You
can do both using the
node-passbook prepare-keys command:
node-passbook prepare-keys -p keys
This is the same directory into which you placet the
Start with a template. A template has all the common data fields that will be shared between your passes, and also defines the keys to use for signing it.
var createTemplate = require"passbook";var template = createTemplate"coupon"passTypeIdentifier: "pass.com.example.passbook"teamIdentifier: "MXL"backgroundColor: "rgb(255,255,255)";
The first argument is the pass style (
eventTicket, etc), and the
second optional argument has any fields you want to set on the template.
You can access template fields directly, or from chained accessor methods, e.g:
templatefieldspassTypeIdentifier = "pass.com.example.passbook";console.logtemplatepassTypeIdentifier;templateteamIdentifier"MXL"passTypeIdentifier"pass.com.example.passbook"
The following template fields are required:
passTypeIdentifier - The Passbook Type ID, begins with "pass."
teamIdentifier - May contain an I
Optional fields that you can set on the template (or pass):
In addition, you need to tell the template where to find the key files and where to load images from:
The last part is optional, but if you have images that are common to all passes, you may want to specify them once in the template.
To create a new pass from a template:
var pass = templatecreatePassserialNumber: "123456"description: "20% off";
Just like template, you can access pass fields directly, or from chained accessor methods, e.g:
passfieldsserialNumber = "12345";console.logpassserialNumber;passserialNumber"12345"description"20% off";
In the JSON specification, structure fields (primary fields, secondary fields, etc) are represented as arrays, but items must have distinct key properties. Le sigh.
To make it easier, you can use methods like
will do the logical thing. For example, to add a primary field:
passprimaryFieldsadd"date" "Date" "Nov 1";passprimaryFieldsadd key: "time" label: "Time" value: "10:00AM";
You can also call
add with an array of triplets or array of objects.
To get one or all fields:
var dateField = passprimaryFieldsget"date";var allFields = passprimaryFieldsall;
To remove one or all fields:
Adding images to a pass is the same as adding images to a template:
passimagesicon = iconFilename;passiconiconFilename;passloadImagesFrom"images";
You can add the image itself (a
Buffer), or provide the name of a file or an
HTTP/S URL for retrieving the image. You can also provide a function that will
be called when it's time to load the image, and should pass an error, or
and a buffer to its callback.
To generate a file:
var file = fscreateWriteStream"mypass.pkpass";passon"error"console.errorerror;processexit1;passpipefile;
Your pass will emit the
error event if it fails to generate a valid Passbook
file, and emit the
end event when it successfuly completed generating the
You can pipe to any writeable stream. When working with HTTP, the
method will set the content type, pipe to the HTTP response, and make use of a
callback (if supplied).