• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

Receive enter and exit events

Live Tracking API can be used to get real time alerts based on asset movement and activity. In this example we will present a step by step guide of how to receive events on a configured webhook, when an asset moves into or out of an area of interest.

Get Started

Readers would need a valid NextBillion API key to try this example out. If you don’t have one, please contact us to get your API key now!

APIs used

  • Live Tracking API

  • Geofence API

Setup

Once you have a valid API Key, you can start setting up the components to be used in this example. Let’s take a look at them below.

Step 1: Create Asset

Having received a valid key, let’s kick start the setup process by creating an asset whose activity we want to track. To create an asset, we will

  • Give a meaningful name to asset

  • Add the attributes of the asset that will be used to associate the asset to a monitor

  • Though we are not specifying any custom ID in this example, but user’s can provide a custom ID to the asset, if preferred.

Following is the Asset JSON that is used for this example:

1curl --location 'https://api.nextbillion.io/skynet/asset?key=<your_api_key>'
2--header 'Content-Type: application/json'
3--data '{
4 "name": "Staff Transportation 001",
5 "description": "Live Tracking Enter & Exit Event Example",
6 "attributes": {
7 "driver_contact_no": "422-583-651",
8 "driver_name": "John Luther",
9 "license": "2 DS 3236",
10 "vehicle_type": "SUV"
11 }
12}'

As soon as the asset is created, we get the asset ID in the acknowledgement response. Please record this ID. For our example, the asset ID is:

1{
2"status": "Ok",
3"data": {
4"id": "eb51687b-2f1d-4a50-bae5-73a6d6e5e77f"
5}
6}

Step 2: Bind a device to the Asset

Next step is to bind a GPS device to the asset which can upload the location and other information for the asset. Please note that the same device can be used for multiple assets, but one asset can be attached to only one device at a time.

1curl --location 'https://api.nextbillion.io/skynet/asset/eb51687b-2f1d-4a50-bae5-73a6d6e5e77f/bind?key=<your_api_key>' \
2--header 'Content-Type: application/json' \
3--data '{
4 "device_id":"APL-14-iOS-X43V21F"
5}'

As soon as the binding process is successful, we get an acknowledgement response from the API:

1{
2"status": "Ok"
3}

Step 3: Set up a Geofence

Geofences are geographic boundaries that are used to mark an area of interest. For our example we create a new geofence using NextBillion.ai’s Geofence API. We create a polygon type geofence using geoJSON input around an area of interest in Los Angeles city. Live Tracking API can work seamlessly with any other types of geofences - isochrone or circle - as well. Readers are encouraged to try other types of geofence as well.

For our geofence, we specify:

  • The type of the geofence as polygon

  • name of the geofence

  • geojson object with details of the coordinates forming the polygon. If the readers are creating their own polygons, it is worth mentioning that the geofence should be closed, not contain any intersecting boundaries, and should not contain other polygons.

Below is the ‘Create a Geofence’ JSON script we use for this example:

1curl --location 'https://api.nextbillion.io/geofence?key=<your_api_key>' \
2--header 'Content-Type: application/json' \
3--data '{
4 "polygon":{
5 "geojson":{
6 "type":"Polygon",
7 "coordinates":[
8 [
9 [
10 -118.268312,
11 34.046253
12 ],
13 [
14 -118.269707,
15 34.045097
16 ],
17 [
18 -118.270963,

Once the geofence is created, we get the asset ID in the acknowledgement response. Please record this ID. For our example, the asset ID is:

1{
2"status": "Ok",
3"data": {
4"id": "7d1b861c-67b8-4128-aef8-179a605263c4"
5}
6}

Step 4: Create a Monitor

Now that we have an asset and a geofence, let’s set up a monitoring criteria using a monitor. To create a monitor, we need to:

  • Specify the type of activity that we want to track. Since we want to track both events of asset entering and leaving the geofence, we set this value to enter_and_exit. Readers can set up only enter or only exit type of monitor, if they wish.

  • Specify the ID of the geofence that we want to use for creating events.

  • Ensure that asset is successfully linked with monitor. It is worth noting that only those assets which share all of their attributes with a monitor are linked with it. For our example, we add the same attributes for the monitor as that of the asset for the sake of simplicity.

  • Optionally, add name and description for the monitor.

Below is the JSON we built for creating the monitor:

1curl --location 'https://api.nextbillion.io/skynet/monitor?key=<your_api_key>' \
2--header 'Content-Type: application/json' \
3--data '{
4"type": "enter_and_exit",
5"geofence_config": {
6"geofence_ids": ["7d1b861c-67b8-4128-aef8-179a605263c4"]
7},
8"name": "Monitor The Ritz Hotel, Los Angeles",
9"description": "Track activity related to free staff transportation service",
10"match_filter":{
11"include_all_of_attributes":{
12 "driver_contact_no": "422-583-651",
13 "driver_name": "John Luther",
14 "license": "2 DS 3236",
15 "vehicle_type": "SUV"
16 }
17}
18}'

Please record the monitor ID from the API response:

1{
2"status": "Ok",
3"data": {
4"id": "dae3b4bd-2402-4b53-83dd-c094b79d02cc"
5}
6}

Now that our asset and monitoring set up is done, we move to the next part that deals with capturing the events once they are created. Live Tracking API uses webhooks to share the event information. Let’s see how to configure a webhook.

Step 5: Configure a Webhook

Readers should obtain a webhook URL from their application where they want to receive the information of events created. In our example, we are using a webhook URL provided by a messenger. Using this webhook results in the event information being sent to the messenger as a notification whenever an event is created.

Although we use a test webhook URL - "https://my-company/api/test_webhook" - in this tutorial to guide readers through the process, we recommend using an actual webhook URL while trying this tutorial.

Once we obtain a valid webhook URL, we register it with our Live Tracking API using the Add or Update Webhook Configuration method.

1curl --location --request PUT 'https://api.nextbillion.io/skynet/config?key=<your_api_key>' \
2--header 'Content-Type: application/json' \
3--data '{ "webhook":[""https://my-company/api/test_webhook""] }'

The registration is acknowledged with an Ok response from the API.

Step 6: Test the webhook

In order to make sure that our webhook is correctly configured and is ready to receive the event information, let’s test it out. We will send a test request using our Test Webhook Configuration method and check if a sample event was received on the webhook or not.

1Test request: curl --location --request POST 'https://api.nextbillion.io/skynet/config/testwebhook?key=<your_api_key>'

Upon sending the above request we received the following sample event on the registered webhook:

1Asset f1ceb76d-2665-4f72-bc56-aae91c4953e3 Trigger enter event at 2022-09-08 09:31:19 Data: {
2"asset_id":"f1ceb76d-2665-4f72-bc56-aae91c4953e3",
3"geofence_id":"350706bf-cc3b-4e74-a66c-c7f050944c47",
4"monitor_id":"8a8ecd4b-f8b2-49ed-8097-b114ce53e4db",
5"monitor_tags":["america","newyork"],
6"event_type":"enter",
7"timestamp":1662629479859,
8"triggered_timestamp":1662629479810,
9"triggered_location":{
10"location":
11{"lat":40.7128,
12"lon":-74.0059},
13"timestamp":1662629479859,
14"accuracy":20,
15"speed":80,
16"bearing":90,
17"altitude":100
18},

Upload non-intervening locations

Now that setup is done, let’s start uploading the locations for the asset’s movement. Initially we will only upload the locations which do not trigger an enter or exit type of event. We will start from a point outside the geofence and remain outside it

We use the following JSON to upload 3 locations for the asset. In a practical scenario, these updates would be automatically sent by the GPS device that is bound to the given asset.

1curl --location 'https://api.nextbillion.io/skynet/asset/eb51687b-2f1d-4a50-bae5-73a6d6e5e77f/track?key=<your_api_key>' \
2--header 'Content-Type: application/json' \
3--data '{
4 "locations":[
5 {
6"location": {
7"lat": 34.044029708304464,
8"lon": -118.27547605693337
9},
10"timestamp": 1686729177000,
11"accuracy": 2,
12"speed": 5,
13"bearing": 25,
14"altitude": 110.5,
15"meta_data": {
16"test": "location_outside_geofence"
17}
18},

Once the locations are uploaded, readers can use the Track locations of an Asset method and verify the track information using the following request:

1curl --location 'https://api.nextbillion.io/skynet/asset/eb51687b-2f1d-4a50-bae5-73a6d6e5e77f/location/list?key=<your_api_key>&geometry_type=geojson'

Create Enter Event

Taking the journey of our asset ahead we start off from the point we left and then take it to a point inside the geofence that we created in Step 3. Following is the JSON we use at this step to upload track information:

1curl --location 'https://api.nextbillion.io/skynet/asset/eb51687b-2f1d-4a50-bae5-73a6d6e5e77f/track?key=<your_api_key>' \
2--header 'Content-Type: application/json' \
3--data '{
4"locations":[
5{
6"location":{
7"lat": 34.045012,
8"lon": -118.269904
9},
10"timestamp":1694930400000,
11"accuracy":2,
12"speed":5,
13"bearing":25,
14"altitude":110.5,
15"meta_data":{
16"test": "location_outside_geofence"
17}
18},

As soon as the first location inside the geofence is uploaded the enter criteria of the monitor is satisfied and the following event information is received on our configured webhook.

1Asset eb51687b-2f1d-4a50-bae5-73a6d6e5e77f Trigger enter event at 2023-09-17 06:00:10 Data: {
2"asset_id":"eb51687b-2f1d-4a50-bae5-73a6d6e5e77f",
3"geofence_id":"7d1b861c-67b8-4128-aef8-179a605263c4",
4"monitor_id":"dae3b4bd-2402-4b53-83dd-c094b79d02cc",
5"monitor_tags":[],
6"event_type":"enter",
7"timestamp":1694930410000,
8"triggered_timestamp":1694930410000,
9"triggered_location":
10{"location":
11{"lat":34.044143356537376,
12"lon":-118.26809250500608},
13"timestamp":1694930410000,
14"accuracy":1,
15"speed":8,
16"bearing":25,
17"altitude":110.5,
18"meta_data":

Create Exit Event

On the next leg of the journey of our asset we start off from a point inside the geofence and move towards exiting it. Following is the JSON we use at this step:

1curl --location 'https://api.nextbillion.io/skynet/asset/eb51687b-2f1d-4a50-bae5-73a6d6e5e77f/track?key=<your_api_key>' \
2--header 'Content-Type: application/json' \
3--data '{
4"locations":[
5{
6"lat": 34.04560182867391,
7"lon": -118.26626376149477
8},
9"timestamp":1694930430000,
10"accuracy":7,
11"speed":12,
12"bearing":25,
13"altitude":100.5,
14"meta_data":{
15"test": "location_inside_geofence"
16}
17},
18{

Notice that the first 2 points are inside the geofence and the last point is outside the geofence that we had created. As soon as the last point is uploaded, we get following event payload sent to the configured webhook URL:

1Asset eb51687b-2f1d-4a50-bae5-73a6d6e5e77f Trigger exit event at 2023-09-17 06:00:40 Data: {
2"asset_id":"eb51687b-2f1d-4a50-bae5-73a6d6e5e77f",
3"geofence_id":"7d1b861c-67b8-4128-aef8-179a605263c4",
4"monitor_id":"dae3b4bd-2402-4b53-83dd-c094b79d02cc",
5"monitor_tags":[],
6"event_type":"exit",
7"timestamp":1694930440001,
8"triggered_timestamp":1694930440000,
9"triggered_location":
10{"location":
11{"lat":34.04704133501632,
12"lon":-118.26470932951017},
13"timestamp":1694930440000,
14"accuracy":7,
15"speed":12,
16"bearing":25,
17"altitude":100.5,
18"meta_data":