Documentation

​
​

Development

User Acquisition

Monetization

Industry

Leaderboards

Unity SDK

Admin API

Client API

Open Unity Dashboard

Leaderboards

LiveOps
​
​
Leaderboards
  • Overview
  • Get started
  • Concepts
  • Tutorials
    • Configuration
    • Unity SDK tutorial
    • Unity SDK sample
    • Cloud Code samples
      • Cloud Code Modules (C#)
      • Cloud Code Script (Javascript)
  • Reference
  • Privacy and consent
  1. Leaderboards

Access Leaderboards via Cloud Code JavaScript Scripts

Refer to code samples that access Leaderboards with the Cloud Code JavaScript Software Development Kit.
Read time 2 minutes
Last updated 8 months ago

You can access leaderboards using the Cloud Code JavaScript Leaderboards SDK. The Cloud Code SDK Documentation Portal provides documentation for all the available methods.
Consider the example where a player invokes a Cloud Code script. The script submits the player's score, retrieves their entry (including their rank) and retrieves the top scores in the leaderboard.
JavaScript:
const { LeaderboardsApi } = require("@unity-services/leaderboards-1.1");module.exports = async ({ params, context, logger }) => { // Retrieve the playerId and accessToken from the context // allows you to ensure requests are scoped to the player // who invoked the Cloud Code script. const { projectId, playerId, accessToken } = context; const { leaderboardId, score } = params; // Initializing the LeaderboardsApi using the accessToken // ensures the requests are made as the player who invoked the // Cloud Code script. const leaderboardsApi = new LeaderboardsApi({ accessToken }); const addScoreResult = await leaderboardsApi.addLeaderboardPlayerScore(projectId, leaderboardId, playerId, { score: score }); const getScoreResult = await leaderboardsApi.getLeaderboardPlayerScore(projectId, leaderboardId, playerId); const getScoresResult = await leaderboardsApi.getLeaderboardScores(projectId, leaderboardId); return { addScoreResultStatus: addScoreResult.status, getScoreResult: getScoreResult.data, getScoresResult: getScoresResult.data };};
Admin level access to player-scoped endpoints is also available through Cloud Code. This allows you to update multiple players’ scores, or a player’s score other than the player who invoked the Cloud Code script, from a single Cloud Code script.
JavaScript:
const { LeaderboardsApi } = require("@unity-services/leaderboards-1.1");module.exports = async ({ params, context, logger }) => { const { projectId } = context; // The below playerId comes from your input parameters. This can // be any player that you wish. const { leaderboardId, playerId, score } = params; // Initialize the LeaderboardsApi using the context allows // for admin-level access to Leaderboards endpoints. const leaderboardsApi = new LeaderboardsApi(context); const addScoreResult = await leaderboardsApi.addLeaderboardPlayerScore(projectId, leaderboardId, playerId, { score: score }); const getScoreResult = await leaderboardsApi.getLeaderboardPlayerScore(projectId, leaderboardId, playerId); const getScoresResult = await leaderboardsApi.getLeaderboardScores(projectId, leaderboardId); return { addScoreResultStatus: addScoreResult.status, getScoreResult: getScoreResult.data, getScoresResult: getScoresResult.data };};

Additional resources

  • Unity Dashboard
  • Cloud Code documentation
  • Unity Authentication

Copyright © 2026 Unity Technologies
LegalPrivacy PolicyCookiesDocumentation Terms of UseDo Not Sell or Share My Personal InformationYour Privacy Choices (Cookie Settings)

"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S and elsewhere (more info here). Other names or brands are trademarks of their respective owners.

Some pages are machine-translated for convenience, and may contain inaccuracies. In the event of conflicting information, the English version is authoritative.

  • On this page
    • Additional resources


Report a problem with this page