Web SDK

One JavaScript file without dependencies for HTML5 games, websites and Node.js servers.

On this page

Requirements#

  • Current versions of Chrome, Edge, Firefox and Safari (desktop and mobile), or Node.js 18+.
  • The page must be served over HTTPS (or localhost): the API requires HTTPS, and Web Crypto, used to verify remote config, only exists on secure pages.

Install#

Load it from your MAJACLOUD server, which serves it with CORS headers:

HTML
<script src="https://majacloud.majanite.com/downloads/majacloud-web-1.0.0.js"></script>
<script type="module">
  const mc = Majacloud.createClient({ baseUrl: 'https://majacloud.majanite.com', projectId: '<project id>', clientKey: 'pk_live_…' });
</script>

Or copy majacloud.js into your project:

JavaScript
// Bundlers (Vite, webpack…): copy majacloud.js into your project
import Majacloud from './majacloud.js';

// Node.js 18+
const Majacloud = require('./majacloud.js');

Create a client#

Only baseUrl, projectId and clientKey are required.

JavaScript
const mc = Majacloud.createClient({
  baseUrl: 'https://majacloud.majanite.com',      // your MAJACLOUD address
  projectId: '<project id>',
  clientKey: 'pk_live_…',     // public, safe in the browser
  configPublicKey: PEM,       // optional: pins Remote Config verification
  storage: 'memory',          // optional: 'memory' or { get, set, remove }; default localStorage
  timeoutMs: 15000,           // optional
  maxRetries: 2,              // optional
});

Results#

Every call resolves to { ok: true, value } or { ok: false, error } and never rejects. value is the REST response body without ok, with the same snake_case fields as the REST reference.

error has code, message, status, requestId, retryAfter, details and retryable.

Events#

Subscribe with on(); it returns a function that unsubscribes.

JavaScript
const stop = mc.auth.on('playerChanged', (player) => {
  document.querySelector('#name').textContent = player ? player.display_name : '';
});
mc.auth.on('sessionExpired', () => showSignIn());
mc.remoteConfig.on('updated', () => applyBalance());

stop(); // every on() returns an unsubscribe function

A leaderboard on your website#

Reading a leaderboard needs no sign-in, so a website can show your top players with the public client key alone:

HTML
<ol id="top-players"></ol>
<script src="https://majacloud.majanite.com/downloads/majacloud-web-1.0.0.js"></script>
<script>
  const mc = Majacloud.createClient({ baseUrl: 'https://majacloud.majanite.com', projectId: '<project id>', clientKey: 'pk_live_…' });
  mc.leaderboards.getTop('coins', { limit: 10 }).then((top) => {
    if (!top.ok) return;
    document.getElementById('top-players').innerHTML = '';
    for (const entry of top.value.entries) {
      const li = document.createElement('li');
      li.textContent = `${entry.player_name} — ${entry.score}`;
      document.getElementById('top-players').append(li);
    }
  });
</script>

Several tabs#

The session is kept in localStorage and shared by all tabs of your site. Refresh tokens rotate, so tabs take turns refreshing (Web Locks) and pick up each other's new tokens: players are never signed out by opening a second tab.

Server-side with the secret key#

On your own Node.js server, create the client with secretKey instead of clientKey to use server.getPlayer() and server.submitScore(). The SDK refuses a secret key in a browser.

JavaScript
// Node.js on your own server only: the secret key never goes to a browser or game build
const Majacloud = require('./majacloud.js');
const admin = Majacloud.createClient({
  baseUrl: 'https://majacloud.majanite.com',
  projectId: process.env.MAJACLOUD_PROJECT_ID,
  secretKey: process.env.MAJACLOUD_SECRET_KEY,
});

const player = await admin.server.getPlayer('player_3f9c…');
await admin.server.submitScore('coins', 'player_3f9c…', 980);

mc.request(method, path, { body, auth }) calls any endpoint without a helper, for example mc.request('GET', '/wallet', { auth: 'player' }).

Search page titles, headings and text.

↑↓ to navigate ↵ to open Esc to close