In this guide, we’ll walk you through the process of sending your first event and reading it back using the Trench Cloud API. The example uses the Trench JavaScript client, but the same can be achieved by calling the Events API directly.

1

Request Access to Trench Cloud

To get started with Trench Cloud, you need to request access from our website. Once you have access, you will receive your API keys and other necessary credentials.

2

Install Trench JavaScript Client

First, you need to install the Trench JavaScript client using your favorite package manager:

npm install trench-js

Alternatively, you can use the hosted version via the script tag below and skip the Initialize the Client step:

<script>
  (function(d,w){if(!w.Trench){w.Trench=function(c){var i={_config:c,_q:[]},m=["track","page","identify","group","getEvents","executeQueries"];m.forEach(function(f){i[f]=function(){i._q.push([f,arguments])}});return i;}; 
  window.trench=new Trench({
    publicApiKey: 'YOUR_PUBLIC_API_KEY',
    serverUrl: 'YOUR_SERVER_URL', 
    autoCaptureEvents: true
  });
  var s=d.createElement("script");s.type="text/javascript";s.async=!0;s.src="https://cdn.jsdelivr.net/npm/trench-js@latest/dist/trench.min.js";
  s.onload=function(){var r=new Trench(trench._config);trench._q.forEach(function(q){r[q[0]].apply(r,q[1])});w.trench=r;};
  d.getElementsByTagName("head")[0].appendChild(s);}})(document,window);
</script>
3

Initialize the Client

After installing the client, you need to initialize it with your API key. Replace YOUR_API_KEY and YOUR_SERVER_URL with the actual API key and server URL you received:

import Trench from 'trench-js'

const trench = new Trench({
  publicApiKey: 'YOUR_PUBLIC_API_KEY',
  serverUrl: 'YOUR_SERVER_URL'
});
4

Send a Sample Event

Now you can send a sample event to Trench Cloud. Here is an example of how to send an event:

trench.track({
  userId: '550e8400-e29b-41d4-a716-446655440000',
  event: 'ConnectedAccount',
  properties: {
    totalAccounts: 4,
    country: 'Denmark'
  }
}).then(response => {
  console.log('Event sent successfully:', response);
}).catch(error => {
  console.error('Error sending event:', error);
});
5

Verify the Event

You can verify that the event was received by querying the events endpoint. Use your private API key for this:

curl -i -X GET \
   -H "Authorization: Bearer private-YOUR_PRIVATE_API_KEY" \
   'YOUR_SERVER_URL/events?event=ConnectedAccount'

This will return a JSON response with the event that was just sent.

Going Further

Now that you’ve sent your first event, you can learn more about the many things you can do with Trench.