Created
February 26, 2018 14:09
-
-
Save hpihkala/ef7590ac2d5a7cb60b16617992e2ef98 to your computer and use it in GitHub Desktop.
Produce data to Streamr over HTTP (example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('node-fetch'); | |
// Stream ID | |
const TEST_STREAM = 'MY_STREAM_ID' | |
// User API key (OR anonymous Stream key with write permission to the stream) | |
const TEST_KEY = 'MY_KEY' | |
// Function to POST data points to the API | |
async function produceToStreamr(msg, streamId, apiKey) { | |
const res = await fetch('https://www.streamr.com/api/v1/streams/'+streamId+'/data', { | |
method: 'POST', | |
headers: { | |
'Authorization': 'token '+apiKey | |
}, | |
body: JSON.stringify(msg) | |
}) | |
if (res.ok) { | |
console.log('%d Success!', res.status) | |
} else { | |
const json = await res.json() | |
console.log('%d %o', res.status, json) | |
} | |
} | |
// Produce a data point | |
let msg = { | |
long: 48.1234, | |
lat: 8.4546, | |
value: 20 | |
} | |
produceToStreamr(msg, TEST_STREAM, TEST_KEY) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment