TikTok for Developers

Developer Docs

Last updated November 13, 2026

Direct Message Sharing

Let players share your mini game with friends through TikTok direct messages. There are two ways to share:

  • Contact picker: Call TTMinis.game.shareAppMessage from the main game domain to open TikTok's contact picker. No open data domain is required.
  • Share to a specific friend: Call tt.shareToUser from the open data domain to share to a friend you identify by openid, for example from a friend leaderboard.

Prerequisites

Before implementing direct message sharing, make sure you have the following:

  • A mini game registered on the Developer Portal
  • TikTok version 40.3.0 or later
  • For sharing to a specific friend only: an open data domain set up as described in the open data domain setup.

Version compatibility check

Use the canIUse API to verify that the user's client supports the sharing functionality:

// Check if shareAppMessage is supported
const isSupported = TTMinis.game.canIUse('shareAppMessage');
if (!isSupported) {
  // Fallback behavior or prompt user to update
  console.log('Please update TikTok to the latest version to use this feature.');
}

Architecture overview

The following architecture applies to sharing to a specific friend. The contact picker runs entirely in the main game domain.

Path

API

Runs in

Needs open data domain

What it does

Contact picker

TTMinis.game.shareAppMessage

Main game domain

No

Opens TikTok's contact picker; the user chooses who to send to. The share card comes from platform configuration.

Share to a specific friend

tt.shareToUser

Open data domain

Yes

Sends to one friend you identify by openid, typically from your friend list or leaderboard.

Set up the open data domain

The open data domain is an isolated environment for social data such as friend lists. Set it up if you use tt.shareToUser to share to a specific friend.

You don't need an open data domain to open the contact picker. To let users choose recipients themselves, call TTMinis.game.shareAppMessage from the main game domain.

Project configuration

Add the openDataContext field to your game.json file. The value is the directory that contains your open data domain code:

json

{
  "openDataContext": "openDataContext"
}

The runtime loads openDataContext/index.js as the entry point of the open data domain. Keep your other settings, such as dev.port and app_id, in project.config.json.

Native game engine integration

For games built with Cocos, Laya, Egret, or other native game engines, the open data domain can be configured to work alongside your main game. Refer to your game engine's documentation for specific integration steps.

Canvas configuration

The open data domain renders content on a shared canvas that overlays your main game. You must configure this canvas position and dimensions appropriately.

Set canvas position and dimensions

// Get the shared canvas from the game global context
let sharedCanvas = GameGlobal.tt.getSharedCanvas();
let sharedContext = sharedCanvas.getContext('2d');

// Set the canvas resolution (internal coordinate system)
sharedCanvas.width = 800;
sharedCanvas.height = 800;

// Set the display size and position using CSS-style properties
sharedCanvas.style.width = '80vw';      // 80% of viewport width
sharedCanvas.style.height = '40vh';     // 40% of viewport height
sharedCanvas.style.top = '20vh';        // Position from top
sharedCanvas.style.left = '10vw';       // Position from left

Canvas best practices

  • Set the internal resolution (width/height) high enough for crisp rendering on high-DPI displays
  • Use viewport-relative units (vw, vh) for responsive positioning across different screen sizes
  • Ensure the canvas doesn't obscure critical game UI elements

Mini Game Domain APIs

These methods are called from your main game code to interact with the open data domain and social features.

getOpenDataContext

Retrieves a reference to the open data context for message passing.

const openDataContext = TTMinis.game.getOpenDataContext();
const { postMessage } = openDataContext;

// Send a message to the open data domain
postMessage({
  type: 'show',
  data: {}
});

For native games, use the global tt object:

const { postMessage } = tt.getOpenDataContext();
postMessage({ type: 'show', data: {} });

authorizeOpenContext

Requests authorization to access the user's friend list and social data. This must be called before accessing any social features.

TTMinis.game.authorizeOpenContext({
  // When get_status_only is true, only checks status without showing UI
  get_status_only: false,
  success: (result) => {
    const { code } = result;
    // Use the code for authentication
    console.log('Authorization code:', code);
  },
  fail: (error) => {
    console.error('Authorization failed:', error);
  },
  complete: () => {
    console.log('Authorization flow completed');
  }
});

Native implementation:

tt.authorizeOpenContext({
  get_status_only: false,
  success: (result) => {
    const { code } = result;
  },
  fail: () => {},
  complete: () => {}
});

Parameters

Field

Type

Description

Required

get_status_only

boolean

When true, only checks authorization status without displaying the authorization dialog

No

success

function

Callback on successful authorization

No

fail

function

Callback on authorization failure

No

complete

function

Callback when authorization flow completes (success or failure)

No

Success Callback Result

Field

Type

Description

code

string

Authorization code for authenticating with the open data domain

setUserCloudStorage

Stores user data in the cloud for persistence across sessions and for sharing with friends (e.g., high scores, game progress).

TTMinis.game.setUserCloudStorage({
  data: [
    { key: 'highScore', value: '10000' },
    { key: 'level', value: '15' }
  ],
  success: () => {
    console.log('Data saved successfully');
  },
  fail: (error) => {
    console.error('Failed to save data:', error);
  }
});

Native implementation:

tt.setUserCloudStorage({
  data: [{ key: 'highScore', value: '10000' }],
  success: () => {},
  fail: () => {}
});

Parameters

Field

Type

Description

Required

data

Array<Object>

Array of key-value pairs to store

Yes

data[].key

string

Key name (max 128 characters)

Yes

data[].value

string

Value to store (max 2048 characters)

Yes

success

function

Callback on successful save

No

fail

function

Callback on save failure

No

complete

function

Callback when operation completes

No

removeUserCloudStorage

Removes specific keys from the user's cloud storage.

TTMinis.game.removeUserCloudStorage({
  keyList: ['highScore', 'tempData'],
  success: () => {
    console.log('Data removed successfully');
  },
  fail: (error) => {
    console.error('Failed to remove data:', error);
  }
});

Native implementation:

tt.removeUserCloudStorage({
  keyList: ['highScore'],
  success: () => {},
  fail: () => {}
});

Parameters

Field

Type

Description

Required

keyList

Array<string>

Array of keys to remove

Yes

success

function

Callback on successful removal

No

fail

function

Callback on removal failure

No

complete

function

Callback when operation completes

No

Open Data Domain APIs

These methods are called from within the open data domain (openDataContext/index.js). The open data domain has limited access to system APIs and primarily handles rendering the friend list and processing social data.

onMessage

Listens for messages sent from the main game domain via postMessage.

tt.onMessage((data) => {
  console.log('Received message from main domain:', data);

  switch (data.command) {
    case 'render':
      // Render the friend list
      renderFriendList();
      break;
    case 'hide':
      // Clear the canvas
      clearCanvas();
      break;
    default:
      console.log('Unknown command:', data.command);
  }
});

Animation Frame Methods

The open data domain supports standard animation frame methods for smooth rendering:

// Request an animation frame
const frameId = tt.requestAnimationFrame(() => {
  // Render logic here
  drawFriendList();
});

// Cancel a scheduled animation frame
tt.cancelAnimationFrame(frameId);

Timer Methods

Standard timer methods are available in the open data domain:

// Set a timeout
const timeoutId = tt.setTimeout(() => {
  console.log('Timeout executed');
}, 1000);

// Clear a timeout
tt.clearTimeout(timeoutId);

getSharedCanvas

Gets the shared canvas for rendering the open data domain content.

const canvas = tt.getSharedCanvas();
const ctx = canvas.getContext('2d');

// Set canvas dimensions
canvas.width = 400;
canvas.height = 200;

// Draw content
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvas.width, canvas.height);

createImage

Creates an image object for loading and displaying images in the open data domain.

const image = tt.createImage();
image.src = 'https://example.com/image.png';
image.onload = () => {
  console.log('Image loaded successfully');
  // Draw image to canvas
  ctx.drawImage(image, 0, 0);
};
image.onerror = (error) => {
  console.error('Failed to load image:', error);
};

getFriendCloudStorage

Retrieves cloud storage data for the user's friends. This is useful for building leaderboards.

tt.getFriendCloudStorage({
  keyList: ['score'],
  success: (res) => {
    res.data.forEach((friend) => {
      const name = friend.displayName || 'Player';
      const score = friend.data?.find((item) => item.key === 'score')?.value || '0';
      // Render the name and score
    });
  }
});

function shareToFriend(friend) {
  if (!friend.openId) {
    // Hide the share button for this friend
    return;
  }
  tt.shareToUser({
    openid: friend.openId,
    // Other shareToUser fields
  });
}

Parameters

Field

Type

Description

Required

keyList

Array<string>

Array of keys to retrieve for each friend

Yes

success

function

Callback on successful retrieval

No

fail

function

Callback on retrieval failure

No

complete

function

Callback when operation completes

No

Success Callback Result

The success callback receives an object with a data array. Each item in data describes one friend:

interface FriendCloudStorageData {
  displayName?: string;
  avatarUrl?: string;
  openId?: string;
  data?: Array<{ key?: string; value?: string }>;
}

Field

Type

Description

displayName

string

The friend's display name.

avatarUrl

string

URL of the friend's avatar.

openId

string

The friend's open_id for your mini game. Can be missing; check before using it.

data

Array<object>

Key-value pairs the friend's game stored with setUserCloudStorage. Each object has key and value strings.

getUserCloudStorage

Retrieves the current user's cloud storage data within the open data domain.

GameGlobal.tt.getUserCloudStorage({
  keyList: ['key_test_abc'],
  success: (data) => {
    console.log('User data retrieved:', data);
  },
  fail: (error) => {
    console.error('Failed to get user data:', error);
  },
  complete: () => {
    console.log('User data request completed');
  }
});

setUserCloudStorage (Open Data Domain)

Sets cloud storage data from within the open data domain.

GameGlobal.tt.setUserCloudStorage({
  data: [
    { key: 'key_test_abc', value: 'value_test_abc' }
  ],
  success: (data) => {
    console.log('Data saved:', data);
  },
  fail: (error) => {
    console.error('Failed to save:', error);
  },
  complete: () => {
    console.log('Save operation completed');
  }
});

removeUserCloudStorage (Open Data Domain)

Removes cloud storage data from within the open data domain.

GameGlobal.tt.removeUserCloudStorage({
  keyList: ['key_test_abc'],
  success: (data) => {
    console.log('Data removed:', data);
  },
  fail: (error) => {
    console.error('Failed to remove:', error);
  },
  complete: () => {
    console.log('Remove operation completed');
  }
});

Social sharing methods

shareToUser

Initiates sharing to a specific friend via direct message. This method can only be called from within the open data domain.

tt.shareToUser({
  imageUrl: 'https://example.com/share-image.png',
  openid: 'friend_openid_from_leaderboard',
  subTitle: 'Check out my new high score',
  templateType: 1,
  title: 'Beat my score!',
  success: () => {
    console.log('Share API call succeeded');
  },
  fail: () => {
    console.log('=========> fail');
  }
});

Note: The success callback reports the result of the API call. It doesn't confirm that the message was delivered or that the recipient opened the game.

Parameters

Field

Type

Description

Required

imageUrl

string

URL of the image to display in the share card

Yes

openid

string

OpenID of the friend to share with (obtained from friend list)

Yes

subTitle

string

Subtitle text displayed in the share card

No

templateType

number

Visual template style: 1 or 2

No

title

string

Main title displayed in the share card

No

success

function

Callback on successful share

No

fail

function

Callback on share failure

No

Template types

Two visual templates are available for share cards:

  • Template Type 1: CTA template, standard share card layout with title, subtitle, and image
  • Template Type 2: Interactive template, alternative layout with different visual styling

Choose the template that best fits your game's branding and content.

shareAppMessage

Initiates sharing via direct message from the main game domain, opening the contact selection interface. This method can only be called from the main game domain.

Note: Requires client version 40.3.0 or higher.

TTMinis.game.shareAppMessage({
  query: 'level=1',
  success: () => {
    console.log('Share API call succeeded');
  },
  fail: (error) => {
    console.error(error);
  }
});

Note: The success callback reports the result of the API call. It doesn't confirm that the message was delivered or that the recipient opened the game.

The share card uses the title and image configured for your mini game on the platform. The API doesn't accept parameters that change the card. Use query to pass launch parameters to players who open the game from the shared message.

Parameters

Field

Type

Description

Required

query

string

Query string passed to the mini game when a recipient opens it from the share, in key=value format. Example: level=1.

No

success

function

Called when the share API call succeeds.

No

fail

function

Called when the share API call fails.

No

shareToStory

Allows users to share content to their TikTok story. This method can only be called from the main game domain.

tt.shareToStory({
  success: () => {
    console.log('Shared to story successfully');
  },
  fail: (error) => {
    console.error('Share to story failed:', error);
  }
});

Implementation example

Here are examples showing how to implement the friend list sharing flow:

Share with the contact picker

To let players choose recipients themselves, call TTMinis.game.shareAppMessage from the main game domain. This doesn't use the open data domain:

function shareWithContacts() {
  if (!TTMinis.game.canIUse('shareAppMessage')) {
    // Hide the share button on clients that don't support sharing
    return;
  }

  TTMinis.game.shareAppMessage({
    query: 'source=direct_message',
    success: () => {
      console.log('Share API call succeeded');
    },
    fail: (error) => {
      console.log('Share API call failed', error);
    }
  });
}

Share to a specific friend

To show a friend list and share to one friend, use both domains. The main game domain requests authorization, saves scores, and sends messages to the open data domain. The open data domain renders the friend list and calls tt.shareToUser.

Main Game Domain

class Game {
  private openDataContext: any;

  constructor() {
    // Initialize open data context
    this.openDataContext = TTMinis.game.getOpenDataContext();
  }

  // Show friend list for sharing
  showFriendList() {
    // Request authorization first
    TTMinis.game.authorizeOpenContext({
      get_status_only: false,
      success: (result) => {
        // Send message to open data domain to render friend list
        this.openDataContext.postMessage({
          type: 'showFriendList',
          data: {
            authCode: result.code
          }
        });
      },
      fail: (error) => {
        console.error('Authorization failed:', error);
      }
    });
  }

  // Hide friend list
  hideFriendList() {
    this.openDataContext.postMessage({
      type: 'hide'
    });
  }

  // Save high score
  saveHighScore(score: number) {
    TTMinis.game.setUserCloudStorage({
      data: [
        { key: 'highScore', value: score.toString() }
      ],
      success: () => {
        console.log('High score saved');
      }
    });
  }
}

Open Data Domain (openDataContext/index.js)

// Listen for messages from main domain
tt.onMessage((message) => {
  switch (message.type) {
    case 'showFriendList':
      renderFriendList();
      break;
    case 'hide':
      clearCanvas();
      break;
  }
});

// Get canvas context
const canvas = tt.getSharedCanvas();
const ctx = canvas.getContext('2d');

// Render friend list
function renderFriendList() {
  // Get friend data with cloud storage
  tt.getFriendCloudStorage({
    keyList: ['highScore'],
    success: (result) => {
      const friends = result.data;

      // Clear canvas
      ctx.clearRect(0, 0, canvas.width, canvas.height);

      // Draw friend list
      friends.forEach((friend, index) => {
        const y = index * 80 + 20;

        // Draw avatar
        const avatar = tt.createImage();
        avatar.src = friend.avatarUrl;
        avatar.onload = () => {
          ctx.drawImage(avatar, 20, y, 60, 60);
        };

        // Draw name
        ctx.fillStyle = '#333333';
        ctx.font = '16px sans-serif';
        ctx.fillText(friend.displayName, 100, y + 30);

        // Draw high score
        const score = friend.data?.[0]?.value || '0';
        ctx.fillStyle = '#666666';
        ctx.font = '14px sans-serif';
        ctx.fillText(`Score: ${score}`, 100, y + 50);
      });
    }
  });
}

// Clear canvas
function clearCanvas() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
}

// Handle share to specific user
function shareToFriend(openid) {
  if (!openid) {
    // Skip friends without an openId
    return;
  }
  tt.shareToUser({
    openid: openid,
    title: 'Join me in this game!',
    subTitle: "Let's play together",
    imageUrl: 'https://your-cdn.com/share-card.png',
    templateType: 1,
    success: () => {
      console.log('Share API call succeeded');
    }
  });
}

Best Practices

  1. Version Checking: Always use canIUse to check if sharing features are available before attempting to use them.
  2. Error Handling: Implement proper error handling for all sharing methods to gracefully handle network issues or user cancellations.
  3. Performance: The open data domain runs in a separate context. Minimize message passing between domains and batch operations when possible.
  4. User Experience: Show loading states while fetching friend data and provide clear feedback when sharing succeeds or fails.
  5. Data Privacy: Only request the minimum necessary permissions. Use get_status_only: true to check authorization status without prompting the user when appropriate.
  6. Canvas Management: Ensure the shared canvas is properly sized and positioned to not interfere with game controls.

Troubleshooting

Issue

Possible Cause

Solution

canIUse('shareAppMessage') returns false

Client version too old

Prompt user to update TikTok to version 40.3.0 or higher

Friend list not displaying

Open data domain not configured

Check that game.json contains the openDataContext field and that its value matches the directory name of your open data domain code

Canvas content not visible

Canvas positioning incorrect

Verify canvas style properties (width, height, top, left) are set correctly

Authorization fails

User declined permission

Handle fail callback and provide alternative flow or explanation

Cloud storage data not persisting

Key format incorrect

Ensure keys are strings and values are JSON-serializable

Share card not displaying correctly

Image URL inaccessible

Verify image URL is publicly accessible and properly formatted

TikTok for Developers