# Kongregate API

**URL:** <https://forum.defold.com/t/kongregate-api/16427>\
**Category:** Questions\
**Created:** [February 1, 2018, 2:51pm UTC](https://forum.defold.com/t/kongregate-api/16427 "2018-02-01T14:51:12Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![ross.grams](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@ross.grams](https://forum.defold.com/u/ross.grams)\
**Post date:** [May 29, 2019, 8:59pm UTC](https://forum.defold.com/t/kongregate-api/16427/5 "2019-05-29T20:59:47Z")

</div>

An update with some more specifics, since I am messing around with Kongregate games again.

Now I am using [JSToDef](https://forum.defold.com/t/jstodef-simple-way-to-send-messages-from-js-to-lua/48527), which is not really necessary for what I’m doing, but it makes life easy. 🙂 So the first step is to add that as a dependency in your game.project file.

Next I added this little snippet to my `index.html`:

```lua
<script src='https://cdn1.kongregate.com/javascripts/kongregate_api.js'></script>
<script id='kongwrapper' type='text/javascript'>
	var isKongLoaded = false;

	function loadKongregate() {
		kongregateAPI.loadAPI(
			function() {
				kongregate = kongregateAPI.getAPI();
				isKongLoaded = true;
				console.log("Kongregate API Loaded");
				JsToDef.send("kongIsLoaded")
			}
		);
	}

	function kongIsLoaded() {
		return isKongLoaded;
	}
</script>

```

(The second function I haven’t actually used yet, but I thought I might want it in the future.)

I put it near the end, but definitely before this:

```lua
<script id='engine-start' type='text/javascript'>
	load_engine();
</script>

```

Then, here is my little test script that loads the Kongregate API, listens for the callback when it is done loading, and gets the current Kongregate username and userID (and sets some labels with them).

```lua

html5 = html5 or { run = function() return end }

local function getKongUser()
	local userName = html5.run("kongregate.services.getUsername()")
	local userID = html5.run("kongregate.services.getUserId()")
	return userName, userID
end

local function jsListener(self, message_id, message)
	if message_id == "kongIsLoaded" then
		local userName, userID = getKongUser()
		label.set_text("#username", "Hello, " .. tostring(userName) .. ".")
		label.set_text("#userid", tostring(userID))
	end
end

function init(self)
	if jstodef then
		jstodef.add_listener(jsListener)
		html5.run("loadKongregate()")
	end
end

```

Hopefully that’s enough to get someone started. The rest is basically just looking at the Kongregate documentation and calling their functions with `html5.run()`.

* * *

Regarding the performance issues I had before: they seem to have disappeared. Maybe because of updates to Defold or Kongregate or because I was doing something stupid before, I don’t know. I haven’t done any fancy graphics or heavy stuff with my latest prototype, but so far it’s running a solid 60 fps on Kongregate. No problems.

---

_[View the full topic](https://forum.defold.com/t/kongregate-api/16427)._
