Skip to content

Instantly share code, notes, and snippets.

@naamancampbell
Created January 2, 2018 11:35
Show Gist options
  • Save naamancampbell/163308a2343260541ceba999d54c734d to your computer and use it in GitHub Desktop.
Save naamancampbell/163308a2343260541ceba999d54c734d to your computer and use it in GitHub Desktop.
Strava API Subscription Webhook on Azure Functions
{
"bindings": [
{
"type": "httpTrigger",
"direction": "in",
"name": "req",
"authLevel": "anonymous",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
],
"disabled": false
}
const fetch = require('node-fetch');
module.exports = function (context) {
context.log('Webhook was triggered!');
if(context.req.method == 'POST') {
if('owner_id' in context.req.body && 'object_id' in context.req.body) {
// environment variables: Application settings -> Application settings
const url = process.env['API_URL'] +
'?athlete_id=' + context.req.body.owner_id +
'&activity_id=' + context.req.body.object_id;
// post new activity to your API
fetch(url)
.then(function(response) {
context.log(response.text());
}).then(function(body) {
context.res = body.json();
context.log = body.json();
})
.catch(function(error) {
context.res = {
status: 502,
body: error.json() };
context.log('API returned error: ', error);
});
}
} else { // [GET]
if('hub.mode' in context.req.query &&
context.req.query['hub.verify_token'] == 'STRAVA') {
context.res = {
'hub.challenge': context.req.query['hub.challenge'] };
context.log(
'hub.challenge received: ', context.req.query['hub.challenge']);
} else {
context.res = {
status: 400,
body: { error: 'Invalid request: ' + context.req.query.toString() }};
context.log(
'Subscribe verification error: ', context.req.query.toString());
}
}
context.done();
}
{
"name": "<your app>",
"version": "0.3.1",
"description": "Provides the Strava Webhook Subscription functionality for the <your app> app.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Naaman Campbell",
"license": "MIT",
"dependencies": {
"node-fetch": "^1.7.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment