SDK library for the Tickethead API.
npm i tickethead-sdk
import { SDK } from 'tickethead-sdk';
// Production API by default
// For development, use new SDK('https://api.staging.tickethead.io');
const sdk = new SDK();
// Set the appropriate organizer name
// NOTE: This is not needed for `sdk.account` and `sdk.auth` calls
await sdk.useOrganizer('tickethead');
// List all events with ticket configurations and occurrences
const events = await sdk.event
.listEvents({
with: {
ticket_config: true,
occurrence: true
}
});
// Get single event with all details (occurrences, timeslot, ticket configurations, discounts, ...)
const singleEvent = await sdk.event.getEvent({ id: 1234 })
// Create an order with 3 tickets
await sdk.order.createOrder({
format: 'PDF',
orderItem: [{
quantity: 3,
ticketConfigId: 12345
}]
});
Caching options can be provided when initializing the SDK. The full list of options is available at https://axios-cache-interceptor.js.org/config Below is a simple example of how to enable caching for the SDK with the time to live of 5 minutes. By default there is no caching.
import { SDK, BaseUrl } from 'tickethead-sdk';
const sdk = await SDK.build({
baseUrl: BaseUrl.ThStaging,
cache:{
ttl: 5 * 60 * 1000, // 5 minutes caching time
}
})
Requests are retried 3 times by default. You can change the number of retries and the time to wait between retries by providing the retry
option.
The full list of options is available at https://www.npmjs.com/package/axios-retry
import { SDK, BaseUrl } from 'tickethead-sdk';
const sdk = await SDK.build({
baseUrl: BaseUrl.ThStaging,
retry: {
retries: 5,
}
})
It is possible to save the state of the SDK and load it later. For example, when using the SDK in the browser, you could put the serialized state in the local storage and load it when the page is reloaded.
import { SDK, BaseUrl } from 'tickethead-sdk';
const sdk = await SDK.build({
baseUrl: BaseUrl.ThStaging,
})
// Do some operations with the SDK, set the authorization etc.
// Save the state
const state = sdk.serialize()
// Load the state
const sdk2 = await SDK.build(state)
How to initialize the SDK for a validator application: First, if you have the payload from the QR code, you can extract the required data from it.
import { extractAccessDataFromValidatorQrCode } from 'tickethead-sdk';
const qrCodeContent =
'{"url":"https://api.staging.tickethead.io/account/v1/organizer/validator/c0ffeef2-b836-4a0f-9555-eeb99c9c99fd","organizer":"dnd"}'
const data = extractAccessDataFromValidatorQrCode(qrCodeContent)
import { SDK, WalletCredentials } from 'tickethead-sdk';
// This is a part of the validator data you get in the QR code when you go to the Dashboard/Assign validator on an event
// Or you can extract it from the QR code content, as shown above
const accessToken = 'c0ffeef2-b836-4a0f-9555-eeb99c9c99fd'
const baseUrl = 'https://api.staging.tickethead.io' // Or simply BaseUrl.EnvironmentName, ie. BaseUrl.ThStaging
// Initialize the SDK
const sdk = await SDK.build({
baseUrl,
})
const validator = await sdk.validator.getValidatorByToken({
token: accessToken,
})
const credentials = new WalletCredentials(validator.wallet)
// After this, the SDK is ready to be used for privileged calls
await sdk.authorize(credentials, validator.organizer)
After you fetch the credentials and initialize the SDK, you can fetch the QR codes for the tickets:
// initialize the SDK before this
const ticketList = await sdk.event.getQrCodes({
organizer_id: validator.organizer,
id: validator.eventId,
})
// Tickets are grouped by categories and have a sub-object with their QR code content
const ticketQrCodes = ticketList
.flatMap((ticketCategory) => ticketCategory.ticket)
.map((ticket) => ticket.ticketQrCode.content)
Tickethead API uses JWT based authentication and authorization. The account/v1/auth/login
is used for users (or services) with credentials, i.e. username and password.
Tickethead SDK uses a global method for any type of authentication with authorize
. After calling this method, the internal HTTP client updates the authorization headers and successive calls will use the resolved JWT from SDK's API.
import { SDK } from 'tickethead-sdk';
const sdk = new SDK();
// Authorize with username and password
// Permissions are based on the account and are loaded into the instance
await sdk.authorize({ username: 'admin', password: 'S3cr3t123' });
// Log in as guest user with a short-living guest JWT
await sdk.authorize();
The SDK instance contains all properties and methods for authorization, users, events, orders and tickets.
Create a new SDK instance.
new SDK(url: 'http://api.tickethead.io', version: 'v1')
or
await SDK.build({baseUrl: 'http://api.tickethead.io', version: 'v1' }) // Other options are available
Property | Description |
---|---|
account | User, organizer and account management |
auth | Authorization endpoints |
blockchain | Wallet signing operations |
event | Event management |
order | Order, order contact and ticket management |
payment | Payment providers operations |
venue | Venue information and search |
ISC