Last active
June 17, 2024 07:08
-
-
Save bacalj/6b88758501b70bcd6000cea22d827a5d to your computer and use it in GitHub Desktop.
Example ajax calls that work with NameCoach API V4
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
var ncapi = 'https://www.name-coach.com/api/private/v4/'; | |
/* | |
generate and fill in an API token from your NameCoach admin | |
*/ | |
var token = ''; | |
/* | |
You will need to fill in an email address of a person in your NameCoach database. | |
Then, given the participant's email, get their data. | |
*/ | |
var email = ''; | |
var xhr = new XMLHttpRequest(); | |
xhr.open( 'GET', ncapi + 'participants/' + email + '/?include="embeddables"' ); | |
xhr.setRequestHeader('Authorization', token ); | |
xhr.setRequestHeader('accept', 'application/json'); | |
xhr.onload = function() { | |
var responseObj = JSON.parse(xhr.response); | |
console.log(responseObj); | |
} | |
xhr.send(); | |
/* | |
You will need to fill in a name page id from a NamePage | |
Then, given the NamePage id, get the participants and their data. | |
*/ | |
var npageid = ''; | |
var xhr2 = new XMLHttpRequest(); | |
xhr2.open('GET', ncapi + 'name_pages/' + npageid +'/participants'); | |
xhr2.setRequestHeader('Authorization', token ); | |
xhr2.setRequestHeader('accept', 'application/json'); | |
xhr2.onload = function() { | |
var responseObj2 = JSON.parse(xhr2.response); | |
console.log(responseObj2.participants); | |
} | |
xhr2.send(); | |
/* and here's one that shows how to get the embedded player and also how to just get the sound and attach it to a native <audio> element. */ | |
var token = '{get from config}'; | |
var ncapi = 'https://www.name-coach.com/api/private/v4/'; | |
var ncdata = 'participants/[email protected]/?include="embeddables"'; | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', ncapi + ncdata ); | |
xhr.setRequestHeader('Authorization', token ); | |
xhr.setRequestHeader('accept', 'application/json'); | |
xhr.onload = function() { | |
var rawResponse = xhr.response; | |
var responseObj = JSON.parse(rawResponse); | |
var soundUrl = responseObj.participant.recording_link; | |
//console.log(responseObj.participant); | |
var sound = document.createElement('audio'); | |
sound.id = 'audio-player'; | |
sound.controls = 'controls'; | |
sound.src = soundUrl; | |
sound.type = 'audio/mpeg'; | |
document.getElementById('rendermehere').appendChild(sound); | |
//console.log(responseObj.participant.embed_image); | |
document.getElementById('rendermehere').innerHTML = responseObj.participant.embed_iframe; | |
} | |
xhr.send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment