Kongregate Developers

Kongregate API Integration for Flash IFrame Games

In order to use the Kongregate Client API in your Flash game when it is hosted in an iframe, you will need to pass some Kongregate-specific Flash variables into your SWF.

Preparing your HTML

In order to pass in the appropriate Flash Variables, your page source will need to be modified slightly. The first step is including some Kongregate Javascript:

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

Once this script is loaded, one can access a string of Kongregate Flash Variables with the following command:


This function will return a URL-encoded string, which will end in "&", so you can simply add any custom Flash Variables you need onto the end of it. You must then pass these Flash Variables into your SWF. For more information on how to do this, see this link

If you are using something like SWFObject to embed your SWF, it might be more convenient to have access to an Object as opposed to a String containing the Flash Variables. This allows you to enumerate the required Kongregate variables and merge them in with your own variables. You can access this object using the flashVarsObject function as shown below:


Basic HTML Example

Here is a very basic example of how to utilize the above information to pass the Kongregate Flash Variables into your SWF. Please note that this example is very crude, and it is more common to use something such as SWFObject or Adobe's tools to embed a SWF.

<script type="text/javascript" src="http://www.kongregate.com/javascripts/kongregate_api.js"></script>
<script language="javascript" type="text/javascript">
  var flashvars = kongregateAPI.flashVarsString();
  var html='<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
  html += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"';
  html += 'WIDTH="550" HEIGHT="400" id="game"><PARAM NAME="movie" ID="movie" VALUE="http://www.mysite.com/MyGame.swf">';
  html += '<PARAM NAME="flashvars" VALUE="' + flashvars + '">'; // Add in flashvars
  html += '<EMBED src="http://www.mysite.com/MyGame.swf" WIDTH="550" HEIGHT="400"';
  html += 'NAME="game" ID="game" TYPE="application/x-shockwave-flash"';
  html += 'PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"';
  html += 'FLASHVARS="' + flashvars + '"></EMBED></OBJECT>'; // Add in flashvars

Preparing your SWF

You will need to make a call to Security.allowDomain(api_path) where api_path is a variable containing the URL to the Kongregate API SWF. The URL to the API is stored in the Flash Variable named kongregate_api_path.

It is typically most convenient to do this in the first SWF that is loaded for your game, and then pass a reference to the API object into any other SWF that will need it. Note that you will need to do this allowDomain call in any SWF that references the Kongregate API as well.

This call needs to be done before you load the API SWF. This will allow communication between your parent SWF and our API. For more information about Security.allowDomain, see this link

Here is a simple example of loading the AS3 API that sets permissions properly:

import flash.display.LoaderInfo;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.system.Security;

var kongregate:*;

// Function that loads the Kongregate API
function initializeKongregateAPI():void{
	// Pull the API path from the FlashVars
	var paramObj:Object = LoaderInfo(root.loaderInfo).parameters;
	var apiPath:String = paramObj.kongregate_api_path || "http://www.kongregate.com/flash/API_AS3_Local.swf";
	// Load the API
	var request:URLRequest = new URLRequest(apiPath);
	var loader:Loader = new Loader();

// Called when the Kongregate API is loaded
function onKongregateAPILoaded( event:Event ):void{
       kongregate = event.target.content;

// Start the process of loading the API:

If you pass the kongregate object to another SWF, you will need to explicitly call allowDomain from that SWF as well. You can either do this with a reference to the API URL string, or you can get it from the API object itself:


If you still can't get things to work, or find you are getting security exceptions, it also might be worth trying Security.allowDomain("*") temporarily to help figure out where problems are happening.

You're all set!

You should now be able to utilize the Kongregate API just as if your game was hosted on Kongregate.