TwinrDocs
Push and engagement / Twinr Push API

Twinr Push API

Send a push notification to one specific person from your own backend, using their device token.

Campaigns in Engage send to everyone, or to a segment. The Twinr Push API is for the other case: a notification aimed at one person, sent by your own code when something happens — an order ships, a message arrives, a booking is confirmed.

The Push API is included with every premium plan.

What you need to set up

Three things, in this order:

  1. Turn on Personalized push notification in Engage.
  2. Collect device tokens from the app with the JS bridge, and store them against your users.
  3. Send from your backend, authenticating with an account secret.

1. Turn it on in Engage

Open Engage, click + Create Campaign, and choose Personalized push notification.

Switch on Personalized push notification and click Save as draft. There is no URL to enter. Your website gets each device token from the app, as described in step 2.

This needs Firebase push notifications turned on first.

2. Get the device token

Inside the app, ask for the token and save it against the signed-in user:

JAVASCRIPT
if (typeof twinr_push_token_fetch === 'function') {
  const result = await twinr_push_token_fetch();
  if (result?.token) {
    await saveTokenForCurrentUser(result.token, result.platform);
  }
}

It returns the token and the platform (android or ios), or nothing if push is not available on that device.

Store tokens against the user, not the session, and allow more than one — a person with a phone and a tablet has a token for each.

Guard the call. twinr_push_token_fetch exists only inside the Twinr app. The typeof check keeps your website working normally in an ordinary browser.

See Push Notification - Device Token for more on the bridge call.

3. Send the notification

Create an account secret

The API authenticates with an account secret. Go to My Profile → Account secrets, click Create secret, and scope it to the organization — or to just the apps this server should be able to reach.

Copy the secret before closing the dialog. Twinr shows it once and cannot show it again.

See Account Secrets for the full walkthrough.

Make the call

Pass the secret as a Bearer token:

Code
POST https://msengageapi.twinr.dev/v3/send-notifications
Authorization: Bearer twnr_live_xxxxxxxxxxxx
Content-Type: application/json
JSON
{
  "application_id": "your-twinr-application-id",
  "push_tokens": ["token1", "token2"],
  "push_title": "Your order has shipped",
  "push_message": "Track it in the app.",
  "click_action": { "type": "open_app" }
}

application_id, push_tokens, push_title, push_message and click_action are required. push_image is optional and takes an image URL.

Your application_id is in your Twinr dashboard: open your app in the Twinr Builder and copy the ID from the address bar. The secret must be scoped to this app or to its whole organization.

Send from your server, never from the browser. Anything in your website's JavaScript is visible to visitors, and anyone holding your secret can send notifications that appear to come from your app. Keep it in an environment variable on the server and send from there.

What tapping the notification does

click_action takes one of exactly three shapes.

JSON
{ "type": "open_app" }

Opens the app wherever the person left it.

Example

Sending to one user from a Node backend:

JAVASCRIPT
async function notifyUser(userId, title, message, url) {
  const tokens = await getTokensForUser(userId);
  if (tokens.length === 0) return;

  await fetch('https://msengageapi.twinr.dev/v3/send-notifications', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.TWINR_SECRET}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      application_id: process.env.TWINR_APPLICATION_ID,
      push_tokens: tokens,
      push_title: title,
      push_message: message,
      click_action: url ? { type: 'url', url } : { type: 'open_app' },
    }),
  });
}

Common Questions

01Nothing arrives when I test.

Work through these in order: Personalized push notification must be enabled in Engage, your account secret must be scoped to this app, the person must have allowed notifications on the device, you must have a stored token for them, and the test must run on a real device — push does not work in a browser or the previewer.

A 401 means the secret is wrong, revoked or expired. A 403 means the secret is valid but not scoped to this app.

02How many tokens can I send to at once?

push_tokens takes a list, so one call can reach several devices. Send a person's own devices together rather than one call each.

03What if someone signs out, or reinstalls?

Tokens change. Save the token each time someone signs in, and remove tokens for a user when they sign out so their notifications do not follow them to a shared device.

04Can I use Zapier or a similar tool?

Yes. Anything that can send a POST request with a JSON body and an Authorization header will work.

05Is this the same as a campaign?

No. Campaigns are set up in Engage and sent to everyone or to a segment. The Push API is for notifications your own code triggers for one person. See Push.