How to use your Garmin watch to tell your team you’re going for a run
Tags: rstats, node, api, garmin
Published: 2020-01-08 21:26:10

---
title: How to use your Garmin watch to tell your team you’re going for a run
published: true
date: 2020-01-08 00:00:00 UTC
tags: rstats, nodejs, api, garmin
canonical_url: https://colinfay.me/api-call-garmin-watch/
---

Building an API in NodeJS and R to send message to Slack from yourGarmin watch.

## Why on earth

ThinkR is a remote company, meaning that we all work from our home. Ontop of other cool things about remote work, this allows me to skip mylunch break and take a one hour break in the middle of the afternoon forsport. And I usually go for a run around 2 or 3 pm, but that moment inthe day is not exactly the same every day. And most of the time, Iforget to tell everyone that I’m leaving the office. I recently jokedthat my 2020 resolution was that I’ll be more strict about telling whenI arrive and leave the “office”.

I was sure it can be done straight from my watch. And guess what, itcan!

## The Slack Part

The slack API is pretty amazing and allows you to use a personal webhook and a curl call to send messages to a selected channel on Slack.

Note: there are several packages in R that can be used to send messagesto Slack, for example `{slackr}`: [https://github.com/hrbrmstr/slackr](https://github.com/hrbrmstr/slackr) &`{slackteams}`: [https://github.com/yonicd/slackteams](https://github.com/yonicd/slackteams). But as I justwanted to make a simple, unique call, it was more straightforward towrite it directly.

So:

- Go to `https://api.slack.com/`

- Click on _Start Building_

- Add an app name and add it to a workspace

 ![](https://colinfay.me/assets/img/slackapi1.png)

- Add a new “Incoming Webhooks”, and select the Channel to post in

 ![](https://colinfay.me/assets/img/slackapi2.png)

And Tadaa 🎉 you now have a curl call that looks like this:

```
curl -X POST -H 'Content-type: application/json' 
  --data '{"text":"Hello, World!"}' 
  https://hooks.slack.com/services/YOUR/WEBHOOK/URL/HERE

```

Now time to turn this into an API.

## Node API

Here is a very simple API built in NodeJS:

```
const express = require('express')
const app = express()

app.get('/', function (req, res) {
  const request = require('request');
  
  const options = {
    url: 'https://hooks.slack.com/services/YOUR/WEBHOOK/URL/HERE',
    json: true,
    body: {
      text: "I'm off for a run!"
    }
  };
  
  request.post(options);
  
  res.send('OK')
})

app.listen(9999, function () {
  console.log('API listening on port 9999!')
})

```

## R API

And with R:

```
library(plumber)

#* @get /
function() {
  httr::POST(
    url = 'https://hooks.slack.com/services/YOUR/WEBHOOK/URL/HERE', 
    body = list(
      text = "I'm off for a run!"
    ), 
    encode = "json"
  )
  
}

```

## Adding this to the watch

I discovered that Garmin has a widget called “API calls”, that let youenter an API endpoint, and the API call is done from thewatch.

[https://apps.garmin.com/en-US/apps/ac9a81ab-a52d-41b3-8c14-940a9de37544](https://apps.garmin.com/en-US/apps/ac9a81ab-a52d-41b3-8c14-940a9de37544)

> I just discovered that I can send API calls from my Garmin watch and I'mvery excited about this and that also made me realized that I'mdefinitely a big nerd.[pic.twitter.com/2h4fk8tCnf](https://t.co/2h4fk8tCnf)
> 
> — Colin Fay 🤘 (@\_ColinFay)[January7,2020](https://twitter.com/_ColinFay/status/1214556262660071427?ref_src=twsrc%5Etfw)

<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

So here it is, I’ve got a Widget on my watch that I can use to sendmessage on Slack 🎉

> You know you're a nerd when you deploy an API that posts to Slack fromyour Garmin watch so that you can tell your team that you're going for arun instead of just… you know… typing it.[pic.twitter.com/f2cwn1pxd7](https://t.co/f2cwn1pxd7)
> 
> — Colin Fay 🤘 (@\_ColinFay)[January7,2020](https://twitter.com/_ColinFay/status/1214655430951092224?ref_src=twsrc%5Etfw)

<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>