Tweeting with Twitter API in Phaser


Social integration has become the corner stone of games. It’s great to get 1 player who loves his / her experience in your game, but why not make it easy for them to tween about it?

Twitter is among the top 5 social features recommended to have in your game. Gamespakrs wrote a great post about this, which you can read about here.

So lets go ahead and set place a twitter button on our stage.

create() {
    // create background image
    let background =, 0, 'menuBackground');
    // create twitter image
    let twitter = this.add.image(250, 250, 'twitterLogo')
Twitter Icon

Now we just need to enable the input on the display object, and add a function to the onInputUp callback.

create() {
    twitter.inputEnabled = true; => {
        window.location = ""
twitter example tween

When clicking on this, it will take you to twitter, with the new tweet page already open. There is a slight problem though, it opens in the same tab as the game is being played in. To get around this, we can use the function, in which the first parameters accepted is the url string. Just as a side note, you can’t tell if it will open a new browser or window. That depends entirly on the users browser settings.

We can go a step further than this though. Twitters API allows additional parameters to be sent through as web querey parameters. You can read more on their API here

The twitter tween intent accepts two parameters by standard, message and hashtags. Passing in either or both will pre-fill your tween input box with the information you give it.

Lets go ahead and create a function that will open a new tween and pass some parameters.

tweet(message, hashtags) {
    let uri = '';
    // if both message and hashtags are to be passed
    if(typeof(message) === 'string' && typeof(hashtags) === 'string') {
        uri += '?text=' + message;
        uri += '&hashtags=' + hashtags;
    // if only message is to be passed
    } else if (typeof(message) === 'string') {
        uri += '?text=' + message;
    // if only hashtags are to be passed
    } else if (typeof(hashtags) === 'string') {
        uri += '?hashtags=' + hashtags;
    // open the new window;

Now, to use it, just simply call the function on the input down, and pass your parameters as you see fit. => {
    this.tweet('@sprite-storm awesome game guys!', 'sprite-storm,gaming,phaser');
Twitter tweet box with hashtags and a message

Of course, you can call this function whenever you like. If someone wins a level, maybe when they level up, or complete the game.

Stay tuned for more tutorials. If you have a request for a specific example or tutorial, please email us at