Skip to content

Instantly share code, notes, and snippets.

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 karlgroves/0b03befad2af598a870e0bc8695fe132 to your computer and use it in GitHub Desktop.
Save karlgroves/0b03befad2af598a870e0bc8695fe132 to your computer and use it in GitHub Desktop.
Post the content of a local file to Tenon APIV2 using Axios and Node
// Obvs you have you `npm install axios`
const axios = require('axios');
const fs = require('fs');
const env = 'https://tenon.io/api/v2/'; // NOTE: if you have a private instance of Tenon, you'd change the domain part of this value
const username = 'YOUR_EMAIL_GOES_HERE';
const password = 'PASSWORD_GOES_HERE';
const projectID = 'PROJECTID_GOES_HERE';
const sourceFile = './file.html'; // replace with the path to the file you want to send to Tenon
const callbackUrl = 'CALLBACK_URL_GOES_HERE';
// This part authenticates you, reads the
axios
.post(env + 'auth/', {
username: username,
password: password
})
.then(res => {
console.log('statusCode:' + res.status);
console.log(res.data.access_token);
if(res.status === 200) {
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + res.data.access_token
};
let content = fs.readFileSync(sourceFile, {encoding: 'utf8'});
console.log(content.length);
let body = {
'src': JSON.stringify(content),
'callbackUrl': callbackUrl,
'projectID': projectID
};
axios.post(env, body, {
headers: headers
}).then(res => {
console.log('statusCode:' + res.status);
console.log(res.data.data.responseID);
}).catch(error => {
console.error(error);
});
}
})
.catch(error => {
console.error(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment