Terminal
Install the Spaire JavaScript SDK
To get started, you need to install the Spaire JavaScript SDK and the Spaire Nextjs helper package. You can do this by running the following command:Terminal
Setting up environment variables
Spaire Access Token
To authenticate with Spaire, you need to create an access token, and supply it to Next.js using aSPAIRE_ACCESS_TOKEN environment variable.
You can create an organization access token from your organization settings.
Configuring a Spaire API Client
To interact with the Spaire API, you need to create a new instance of theSpaire class. This class uses the provided access token to authenticate with the Spaire API.
sandbox with production when you’re ready to switch to the production environment.
Generating Spaire Checkout Sessions
Next up, we need to create a checkout endpoint to handle the creation of checkout sessions. Go ahead and create a new GET route in Next.js.Handling Spaire Webhooks
Spaire can send you events about various things happening in your organization. This is very useful for keeping your database in sync with Spaire checkouts, orders, subscriptions, etc. Configuring a webhook is simple. Head over to your organization’s settings page and click on the “Add Endpoint” button to create a new webhook.Tunneling webhook events to your local development environment
If you’re developing locally, you can use a tool like ngrok to tunnel webhook events to your local development environment. This will allow you to test your webhook handlers without deploying them to a live server. Run the following command to start an ngrok tunnel:Terminal
Add Webhook Endpoint
- Point the Webhook to
your-app.com/api/webhook/spaire. This must be an absolute URL which Spaire can reach. If you use ngrok, the URL will look something like this:https://<your-ngrok-id>.ngrok-free.app/api/webhook/spaire. - Select which events you want to be notified about. You can read more about the available events in the Events section.
- Generate a secret key to sign the requests. This will allow you to verify that the requests are truly coming from Spaire.
- Add the secret key to your environment variables.
Terminal
Setting up the Webhook handler
Handling Webhook Events
Depending on which events you’ve subscribed to, you’ll receive different payloads. This is where you can update your database, send notifications, etc.Notifying the client about the event
If you’re building a real-time application, you might want to notify the client about the event. On the confirmation-page, you can listen for thecheckout.updated event and update the UI accordingly when it reaches the succeeded status.

