Skip to content

Instantly share code, notes, and snippets.

@hpihkala
Created February 26, 2018 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpihkala/ef7590ac2d5a7cb60b16617992e2ef98 to your computer and use it in GitHub Desktop.
Save hpihkala/ef7590ac2d5a7cb60b16617992e2ef98 to your computer and use it in GitHub Desktop.
Produce data to Streamr over HTTP (example)
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