# 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:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mudcat](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/m/eada6e/32.png) [@mudcat](https://forum.defold.com/u/mudcat)\
**Post date:** [February 1, 2018, 2:51pm UTC](https://forum.defold.com/t/kongregate-api/16427/1 "2018-02-01T14:51:12Z")

</div>

I looked around the forums and couldn’t find much information on integrating Kongregate API into an HTML5 build.

Is this possible to do? I looked at the following thread: [Javascript HTML5 Extension](https://forum.defold.com/t/javascript-html5-extension/8863)

Can the Kongregate API be integrated via an extension? They have a Javascript API on their site here: [Javascript API](https://docs.kongregate.com/docs/javascript-api)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [February 1, 2018, 3:13pm UTC](https://forum.defold.com/t/kongregate-api/16427/2 "2018-02-01T15:13:14Z")

</div>

Yes, this looks absolutely possible to do! I’m currently in the process of creating a wrapper from another JavaScript API to a Defold Lua API via a native extension and it’s a pretty straight forward process, although fairly time consuming. My recommendation is to narrow down the bare minimum of their API that you need to support and start from there.

---

<div class="post-metadata">

**Author:** ![mudcat](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/m/eada6e/32.png) [@mudcat](https://forum.defold.com/u/mudcat)\
**Post date:** [February 1, 2018, 3:34pm UTC](https://forum.defold.com/t/kongregate-api/16427/3 "2018-02-01T15:34:55Z")

</div>

Great! I will look into it. Thanks 🙂

---

<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:** [February 1, 2018, 3:37pm UTC](https://forum.defold.com/t/kongregate-api/16427/4 "2018-02-01T15:37:30Z")

</div>

Yup, it’s quite easy. You don’t really need a native extension, at least not for the basic stuff, just a tiny bit of javascript to set it up and then use `html5.run()` to call the Kongregate functions. I set up some of the high score and username stuff a while ago, but unfortunately I scrapped it, so I don’t have much to share.

You can follow the Kongregate documentation that you linked. You just need to add the Kongregate API in your index.html file:

```javascript
<script src='https://cdn1.kongregate.com/javascripts/kongregate_api.js'></script>

```

Then load it with `kongregateAPI.loadAPI`, like in the Kong docs. I did this with my own little Javascript file, but you could just add it to the script block in your index.html, or maybe even do it from Lua with `html5.run()`. Then I believe you can just do things like:

```lua
html5.run("kongregate.stats.submit('Score', 1000);")

```

From any of your Lua scripts.

* * *

I am curious to see how Kongregate works for you. I wanted to put my Asteroids game on Kongregate, but when I uploaded a test build, I had HUGE performance issues. The web build I put on Itch runs fine, but the same build on Kongregate ran very slow (inconsistent, and often well under 30 fps). I checked the profiler, tried to optimize my code, tried uploading an empty template project—still slow. I never figured it out. ☹ But . . . it’s quite possible that I was doing something wrong.

---

<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.
