Usage¶
Note
Our documentation uses AGI and API interchangeably. It's the same thing!
Download and embed our AGI script in your game¶
- Visit our developer portal
- Download the latest version of our
ag.min.js
script by looking for theJavascript API Source
link under the Helpful Links sidebar - Include the script in your game's build output
- Add the appropriate
<script>
tag to your game'sindex.html
page so it is loaded on page load
Initialize the API¶
You need to set the document.domain to "armorgames.com" in order for your iframe to have access to our AGI object created on the webpage.
Here is some example code for waiting on the AGI to load and setting the domain correctly.
document.domain = "armorgames.com";
(function() {
var ag = null;
document.addEventListener("DOMContentLoaded", function(event) {
var agiChecks = 0;
function checkForAGI() {
if (agiChecks > 1000) return;
try {
if (typeof parent.agi !== 'undefined') {
ag = new ArmorGames({
user_id: parent.apiAuth.user_id,
auth_token: parent.apiAuth.auth_token,
game_id: parent.apiAuth.game_id,
// Todo: Set the api_key to your game's unique api_key
api_key: '00000000-0000-0000-0000-000000000000',
agi: parent.agi
});
// ... you can start doing AG requests
} else {
agiChecks++;
window.setTimeout(checkForAGI, 250);
}
} catch (err) {
agiChecks++;
window.setTimeout(checkForAGI, 250);
}
}
checkForAGI();
});
})();
Parameter details¶
user_id
: Provided by the game page outside of the containing iframeauth_token
: Provided by the game page outside of the containing iframegame_id
: Provided by the game page outside of the containing iframeapi_key
: Visit the developer portal to obtain this key from the game's settings page
Warning
When calling certain functions, such as consume, or save game, you may notice the first HTTP request fails with a 401 status. This is by design due to how we handle signed requests. A second request is automatically made.
Error handling¶
Attach .catch(function(error) {})
to any of the API calls to capture error conditions.
The API uses promises which should provide a familiar interface.
ag.saveGame('stats_level_1', {
time: 501,
score: 32,
duration: 72
}).then(function(response) {
var key = response.key;
}).catch(function(error) {
console.error('Uh oh: ', error);
});