Home Articles Pitch Api

Contents

Start hereAPI DocsIntroductionSend your first eventEvent parametersStructured EventsActionsContextsError handlingManualIntroductionBasicsConventionsSetupTest modeNotificationsPWA

Sending actions in events

Actions are objects that make events render buttons in the UI. Pressing these buttons will send a webhook to your server.

Here’s a simple example:

🤖
a critical event

12:00 am

const e = {
  name: "a critical event",
  avatar: "🤖",
  actions : [
    {
      url : 'https://webhook.site',
      buttonText : 'Press me'
    }
  ]
};
Copy

You can also have static buttons which simply open up a webpage. Here’s another example:

🤖
a critical event

12:00 am

const e = {
  name: "a critical event",
  avatar: "🤖",
  actions : [
    {
        url : 'https://stripe.com',
        external : true,
        buttonText : 'Press me'
      }
  ]
};
Copy

You can pass Actions as a json array. Each object in the array has these params:

urlstring required

A valid url, eg https://stripe.com, http://server.io, etc.

buttonTextstring required

Set the text of the button, eg: Send email to customer, Activate user, Open in stripe, etc.

keystring required

A unique string to identify the action. Use this on your server to differentiate between different actions. For actions marked as external, no key is needed.

externalboolean

If set, opens the url in a external tab. Show a link icon in the button and behaves like a anchor tag basically.

expireInnumber

Action cannot be run after in ‘expireIn’ minutes. Defaults to 10080(7 days). Max value cannot be more than 43200. We recommend this value to be as small as reasonably possible.

metastring/json

Defaults to null. This can be a string, or a object. This will be passed to the server when this action is triggered.

const e = {
  name: "some event",
  avatar: "🤖",
  type : "rows",
  actions : [
    {
      url : 'https://yourserver.com/api/someroute',
      buttonText : 'Press me',
      key : "press_me",
      external : false,
      expireIn : 10080,
      meta : {
        name : "Shash",
        id : 1,
      }
    },
  ]
};
Copy

How do actions work?

When pressed, our server will send a POST request to the url specified. It will wait for 10 seconds and give up after that. If the server sends back a 200 or a 201 response status, the actions is marked as complete.

// Sample nodejs/fastify code to catch actions
const Fastify = require('fastify');
const fastify = Fastify();

fastify.register(require('fastify-formbody'));

fastify.post('/ops/action', async (request, reply) => {
  try {
    // Access the parsed body
    const { actionType, data } = request.body;

    // Your logic here
    console.log('Received action:', actionType);
    console.log('Data:', data);

    // Respond to the client
    return { status: 'success', received: { actionType, data } };
  } catch (error) {
    // Handle any errors
    console.error('Error processing request:', error);
    return reply.status(500).send({ status: 'error', message: 'Internal Server Error' });
  }
});

// Start the server
const start = async () => {
  try {
    await fastify.listen(3000);
    console.log('Server listening on http://localhost:3000');
  } catch (err) {
    console.error(err);
    process.exit(1);
  }
};

start();
Copy

Actions are great for triggering atomic events. For instance, you can use actions to ban users, or delete a user record. There are multiple safeguards in place to make sure you don’t trigger atomic actions accidently.

Here’s a quick setup for banning users.

const e = {
  name: "some event",
  avatar: "🤖",
  type : "rows",
  actions : [
    {
      url : 'https://yourserver.com/api/someroute',
      buttonText : 'Ban user',
      key : 'ban_user',
      expireIn : 120
    },
  ]
};
Copy

Note the really small expireIn time. This will ensure we cannot run our trigger after 2 hours from now(where it won’t be effective).

Structured EventsContexts