Skip to main content

Traker: the power of API-first and automation

Demo of the iOS Shortcut logging an event to Traker after tapping an NFC tag.
Demo of the iOS Shortcut logging an event to Traker.

In this post, I’ll show you how to leverage the API-first approach of Traker to automate event tracking. Specifically, I’ll guide you through creating an iOS shortcut that creates events whenever you tap an NFC tag.

Introduction #

If you don’t know Traker yet, it’s a simple and flexible event tracking tool that helps you keep track of various activities in your life. Whether it’s tracking habits, logging workouts, or monitoring any other events, Traker provides an easy way to do so. Learn more about it in this other blog post.

One of the standout features of Traker is its API-first design. This means that every action you can perform in the app can also be done programmatically via its API. This opens up a world of possibilities for automation and integration with other tools. You can find the API documentation here.

For example, you can create a new event in the app by tapping the “+” button and filling out the form:

Screenshot of the Traker app showing the form to create a new event.
Creating a new event in Traker.

However, you can also achieve the same result by making a simple HTTP POST request to the Traker API:

curl --request POST \
  --url https://traker.afonso.app/api/events/ \
  --header 'authorization: Bearer [API_TOKEN]' \
  --data '{
  "type_name": "Peso Mousse",
  "value": 4.5,
  "notes": "weight",
}'

This flexibility allows you to integrate Traker with various tools and services, enabling you to automate your event tracking in ways that suit your lifestyle.

In this tutorial, we’ll create an iOS shortcut that logs an event to Traker whenever you tap an NFC tag. This can be particularly useful for tracking activities like workouts, meals, or any other event you want to log quickly.

Prerequisites #

Before we begin, ensure you have the following:

Image of a pack of NFC tags.
Pack of NFC tags.

Step 1: Generate an API Token #

To interact with Traker from outside the app, you’ll need an API token. An API token is a unique identifier that allows you to authenticate your requests to the Traker API.

To generate an API token, go to the API tokens page in your Traker account and tap the + button.

Screenshot of the Traker app showing the form to create a new API token.
Creating a new API token in Traker.

You can give your token a name and set an expiration date if desired. As the name implies, if you set an expiration date, the token will expire after the specified date, and you’ll need to generate a new one to continue using it.

Once you create the token, make sure to copy it and store it securely, as you won’t be able to see it again. Note that the token gives full access to your Traker data, so treat it like a password.

Screenshot of the Traker app showing a newly created API token.
Newly created API token in Traker.

Step 2: Create the iOS Shortcut #

Now that you have your API token, it’s time to create the iOS shortcut that will log events to Traker when you tap an NFC tag.

  1. Open the Shortcuts app on your iOS device and tap the + button to create a new shortcut.

  2. In the shortcut editor, add the action Get contents of URL.

  3. Set the URL to https://traker.afonso.app/api/events.

  4. Change the method to POST.

  5. Tap Headers and add new Header:

    • Key: Authorization
    • Text: Bearer [API_TOKEN] (replace [API_TOKEN] with the API token you generated earlier).
  6. Add new fields to the JSON request body with the following key-value pairs:

    • type_name: The name of the event type you want to log (e.g., “Workout”). If the event type doesn’t exist, it will be created automatically.
    • value (optional): The value associated with the event.
    • notes (optional): Any additional notes you want to include (e.g., “Morning run”).

    Note that instead of passing the type_name directly, you can also pass the type_id if you prefer. You can find the type_id in the details page of the event type in the Traker app.

Screenshot of the iOS Shortcuts app showing the configuration of the “Get contents of URL” action.
Configuring the ‘Get contents of URL’ action in iOS Shortcuts.

  1. You can stop here if you want, however, I recommend adding an extra step to get a confirmation that the event was logged successfully. Add a If action to check the response from the API. Since creating a new event generates a new event ID which is returned in the contents of the URL (response), we can check if it contains the following text string "id":"evt_. If so, we can assume the event was created successfully.

Screenshot of the iOS Shortcuts app showing the configuration of the “If” action.
Configuring the ‘If’ action in iOS Shortcuts.

  1. Inside the If action, add a Show Notification action to notify you that the event was logged successfully. You can customize the notification message as desired.

Screenshot of the iOS Shortcuts app showing the configuration of the “Show Notification” action.
Configuring the ‘Show Notification’ action in iOS Shortcuts.

Now you have a complete shortcut that logs an event to Traker.

Screenshot of the iOS Shortcuts app showing the complete shortcut.
Complete iOS Shortcut to log events to Traker.

You can test it by tapping the play button in the shortcut editor. If everything is set up correctly, you should see a notification confirming that the event was logged successfully and the event should appear in your Traker app.

Screenshot of the iOS Shortcuts app showing a notification confirming the event was logged successfully.
Notification confirming the event was logged successfully.
Screenshot of the Traker app showing the newly logged event.
Newly logged event in Traker.

Step 3: Add NFC Trigger #

The final step is to add an NFC trigger to your shortcut so that it runs automatically when you tap an NFC tag.

  1. In the Shortcuts app go to the Automation tab and tap the + button to create a new automation.

  2. Select the NFC trigger.

  3. Tap Scan and hold your iOS device near the NFC tag you want to use. Once the tag is recognized, give it a name and tap Done.

  4. Select Run Immediately and tap Next.

  5. Then, search for the shortcut you created earlier and select it.

Screenshot of the iOS Shortcuts app showing the configuration of the NFC trigger.
Configuring the NFC trigger in iOS Shortcuts.

Now your automation is set up. Whenever you tap the NFC tag with your iOS device, the shortcut will run, and an event will be logged to Traker.

Demo #

Demo of the iOS Shortcut logging an event to Traker after tapping an NFC tag.
Demo of the iOS Shortcut logging an event to Traker.

In the demo above, you can see how I tap the NFC tag with my iPhone, and a notification appears confirming that the event was logged successfully. You can then open the Traker app to see the newly logged event.

You can use different NFC tags for different event types or values, allowing you to quickly log various activities throughout your day.