Multiplayer API Logo

Multiplayer API

Unity SDK for
Multiplayer Games

Unity SDK

Unity SDK Download

View on GitHub
Repository

Unity API Documentation

Client

Main SDK class for Unity multiplayer functionality

Constructor

public Client(string apiToken, string apiPrivateToken, string baseUrl = "https://api.michitai.com/api", 
               ILogger logger = null, HttpClient httpClient = null, bool useUnityFormat = true)

Initializes the SDK with API tokens and optional Unity formatting.

Player Management

Methods for player registration, authentication, and lifecycle management

RegisterPlayer

public Task<PlayerRegisterResponse> RegisterPlayer<T>(string name, T playerData = null, CancellationToken ct = default)

Registers a new player with optional custom data. Uses generic type for player data with JsonUtility serialization.

AuthenticatePlayer

public Task<PlayerAuthResponse<T>> AuthenticatePlayer<T>(string playerToken, CancellationToken ct = default)

Authenticates a player using their private token. Returns player information with typed data support.

SendPlayerHeartbeatAsync

public Task<PlayerHeartbeatResponse> SendPlayerHeartbeatAsync(string playerToken, CancellationToken ct = default)

Sends a heartbeat to maintain the player's connection. Call every 30-60 seconds to prevent timeout.

LogoutPlayerAsync

public Task<PlayerLogoutResponse> LogoutPlayerAsync(string playerToken, CancellationToken ct = default)

Logs out the current player and invalidates their session. Updates last logout timestamp.

GetAllPlayers

public Task<PlayerListResponse> GetAllPlayers(CancellationToken ct = default)

Lists all registered players. Requires private API token. Useful for admin dashboards.

RenamePlayerAsync

public Task<PlayerRenameResponse> RenamePlayerAsync(string playerToken, string newName, CancellationToken ct = default)

Renames a player to a new name. Validates name length (2-50 characters) and requires player authentication.

BanPlayerAsync

public Task<PlayerBanResponse> BanPlayerAsync(int playerId, EBanTime banDuration, string banReason = null, CancellationToken ct = default)

Bans a player from the game with specified duration (Hour, Day, Week, Month, Quarter, Year, Forever) and optional reason. Requires private API token.

UnbanPlayerAsync

public Task<PlayerUnbanResponse> UnbanPlayerAsync(int playerId, CancellationToken ct = default)

Unbans a previously banned player. Requires private API token.

IsBanned

public static bool IsBanned(ApiResponse response)

Checks if an API response indicates the player is banned. Returns true if the error message contains "You are banned".

Game Data Management

Methods for managing global and player-specific game data

GetGameData

public Task<GameDataResponse<T>> GetGameData<T>(CancellationToken ct = default)

Retrieves global game data with generic type support. Data is automatically deserialized using JsonUtility.

UpdateGameData

public Task<SuccessResponse> UpdateGameData<T>(T data, CancellationToken ct = default)

Updates global game data. Requires private API token. Uses generic type for type safety.

GetPlayerData

public Task<PlayerDataResponse<T>> GetPlayerData<T>(string playerToken, CancellationToken ct = default)

Retrieves player-specific data with generic type support. Requires player authentication.

UpdatePlayerData

public Task<SuccessResponse> UpdatePlayerData<T>(string playerToken, T data, CancellationToken ct = default)

Updates player-specific data with generic type support. Requires player authentication.

Time Management

Methods for server time synchronization

GetServerTime

public Task<ServerTimeResponse> GetServerTime(CancellationToken ct = default)

Retrieves current server time in UTC and readable formats.

GetServerTimeWithOffset

public Task<ServerTimeWithOffsetResponse> GetServerTimeWithOffset(int utcOffset, CancellationToken ct = default)

Retrieves server time with specified UTC offset. Useful for time synchronization.

Room Management

Methods for creating and managing game rooms

CreateRoomAsync

public Task<RoomCreateResponse> CreateRoomAsync<T>(string playerToken, string roomName, int maxPlayers = 4, string password = null, bool hostSwitch = false, bool canLeaveRoom = true, T rules = null, T playerData = null, CancellationToken ct = default)

Creates a new game room with optional password, host switching, player data and rules. The creating player becomes the host. Max players: 2-16.

GetRoomsAsync

public Task<RoomListResponse<T>> GetRoomsAsync<T>(string search = "", int limit = 20, CancellationToken ct = default)

Retrieves all available game rooms with typed rules support. Useful for server browser. Supports search and limit parameters (limit: 1-50).

JoinRoomAsync

public Task<RoomJoinResponse> JoinRoomAsync(string playerToken, string roomId, string password = null, T playerData = null, CancellationToken ct = default)

Joins an existing room with password and player data. Password required for private rooms.

GetRoomPlayersAsync

public Task<RoomPlayersResponse> GetRoomPlayersAsync(string playerToken, CancellationToken ct = default)

Lists all players in the current room with heartbeat status.

LeaveRoomAsync

public Task<RoomLeaveResponse> LeaveRoomAsync(string playerToken, CancellationToken ct = default)

Leaves the current room and updates player status.

SendRoomHeartbeatAsync

public Task<HeartbeatResponse> SendRoomHeartbeatAsync(string playerToken, CancellationToken ct = default)

Sends heartbeat to maintain room connection. Call every 30-60 seconds while in room.

GetCurrentRoomAsync

public Task<CurrentRoomResponse> GetCurrentRoomAsync(string playerToken, CancellationToken ct = default)

Gets comprehensive room state including player lists and pending actions.

StopRoomAsync

public Task<SuccessResponse> StopRoomAsync(string playerToken, CancellationToken ct = default)

Stops current game room (Host Only). Completely removes room and all associated data.

KickPlayerAsync

public Task<RoomKickResponse> KickPlayerAsync(string playerToken, int playerId, CancellationToken ct = default)

Kicks a player from the game room (Host Only). Cannot kick yourself.

UpdateRoomPasswordAsync

public Task<SuccessResponse> UpdateRoomPasswordAsync(string playerToken, string password, CancellationToken ct = default)

Updates the room password (Host Only). Use empty string to remove password.

Room Actions

Methods for submitting and processing game actions

SubmitActionAsync

public Task<ActionSubmitResponse> SubmitActionAsync<T>(string playerToken, SubmitAction<T> request, CancellationToken ct = default)

Submits an action to specific targets (host, all, others, or specific players) with typed request data.

PollActionsAsync

public Task<ActionPollResponse> PollActionsAsync(string playerToken, CancellationToken ct = default)

Polls for completed actions from other players. Call periodically to check for new actions.

GetPendingActionsAsync

public Task<ActionPendingResponse<T>> GetPendingActionsAsync<T>(string playerToken, CancellationToken ct = default)

Gets actions awaiting processing by the current player with typed request data.

CompleteActionAsync

public Task<ActionCompleteResponse> CompleteActionAsync<T>(string actionId, string playerToken, ActionComplete<T> request, CancellationToken ct = default)

Marks an action as completed with processing results using typed response data.

Room Updates

Methods for data synchronization between players

UpdatePlayersAsync

public Task<UpdatePlayersResponse> UpdatePlayersAsync<T>(string playerToken, UpdatePlayers<T> request, CancellationToken ct = default)

Sends updates to specific players or all players. Uses typed UpdatePlayers parameter.

PollUpdatesAsync

public Task<PollUpdatesResponse> PollUpdatesAsync(string playerToken, PollUpdates request, CancellationToken ct = default)

Polls for updates from specific source players. Use PollUpdates request object to specify from_players, from_players_ids, and lastUpdate.

Realtime

WebSocket connections for live multiplayer gameplay

Constructor

public Realtime(string realtimeWebSocketUrl = "wss://realtime.michitai.com")

Initializes realtime WebSocket client with optional custom server URL. Default: wss://realtime.michitai.com

GetTokenAsync (Static)

public static async Task<TokenResponse> GetTokenAsync(
    Client client, 
    string playerToken
)

Static method to generate realtime authentication token using API client.

ConnectAsync

public async Task<bool> ConnectAsync(string realtimeToken)

Connects to WebSocket server using realtime token. Returns true if successful, false if failed. Includes connection state protection.

SendAsync

public async Task SendAsync<T>(
    ERoomTargetPlayer target,
    string command,
    T data = null,
    int[] targetIds = null
)

Sends a message to specific targets (Host, All, Others, or specific player IDs) with optional data. Uses JsonUtility for serialization.

DisconnectAsync

public async Task DisconnectAsync()

Disconnects from WebSocket server and cleans up resources. Automatically called in OnDestroy.

Events

public event Action<string, string, SenderInfo> OnReceive;
public event Action OnConnected;

OnConnected: Fired when WebSocket connection is established. OnReceive: Fired when a message is received from server with command, data, and sender information.

Matchmaking

Methods for matchmaking lobby management and game start

GetMatchmakingLobbiesAsync

public Task<MatchmakingListResponse> GetMatchmakingLobbiesAsync(string search = "", int limit = 20, CancellationToken ct = default)

Lists all available matchmaking lobbies. Useful for lobby browser functionality. Supports search and limit parameters (limit: 1-50).

CreateMatchmakingLobbyAsync

public Task<MatchmakingCreateResponse> CreateMatchmakingLobbyAsync<T>(string playerToken, string matchmakingName, int maxPlayers = 4, bool strictFull = false, bool joinByRequests = false, bool hostSwitch = false, bool canLeaveRoom = true, string password = null, T rules = null, T playerData = null, CancellationToken ct = default)

Creates a new matchmaking lobby with name, optional password, typed rules support, host switching, and player data. The creating player becomes the host. Max players: 2-16.

RequestToJoinMatchmakingAsync

public Task<MatchmakingJoinRequestResponse> RequestToJoinMatchmakingAsync(string playerToken, string matchmakingId, CancellationToken ct = default)

Requests to join a lobby that requires host approval. Returns request ID for tracking.

RespondToJoinRequestAsync

public Task<MatchmakingPermissionResponse> RespondToJoinRequestAsync(string playerToken, string requestId, MatchmakingRequestAction action, CancellationToken ct = default)

Responds to a join request (approve/reject). Only the lobby host can call this.

CheckJoinRequestStatusAsync

public Task<MatchmakingRequestStatusResponse> CheckJoinRequestStatusAsync(string playerToken, string requestId, CancellationToken ct = default)

Checks the status of a join request. Useful for tracking approval/rejection status.

GetCurrentMatchmakingStatusAsync

public Task<MatchmakingCurrentResponse<T>> GetCurrentMatchmakingStatusAsync<T>(string playerToken, CancellationToken ct = default)

Gets comprehensive lobby state with typed rules including player status and pending requests.

JoinMatchmakingDirectlyAsync

public Task<MatchmakingDirectJoinResponse> JoinMatchmakingDirectlyAsync(string playerToken, string matchmakingId, CancellationToken ct = default)

Joins a matchmaking lobby directly. Only works if lobby allows direct join.

LeaveMatchmakingAsync

public Task<MatchmakingLeaveResponse> LeaveMatchmakingAsync(string playerToken, CancellationToken ct = default)

Leaves the current matchmaking lobby and updates player status.

GetMatchmakingPlayersAsync

public Task<MatchmakingPlayersResponse> GetMatchmakingPlayersAsync(string playerToken, CancellationToken ct = default)

Lists all players in the current matchmaking lobby. Useful for displaying player lists.

SendMatchmakingHeartbeatAsync

public Task<MatchmakingHeartbeatResponse> SendMatchmakingHeartbeatAsync(string playerToken, CancellationToken ct = default)

Sends heartbeat to maintain lobby connection. Call every 30-60 seconds while in lobby.

RemoveMatchmakingLobbyAsync

public Task<MatchmakingRemoveResponse> RemoveMatchmakingLobbyAsync(string playerToken, CancellationToken ct = default)

Removes the matchmaking lobby and kicks all players. Only the lobby host can call this.

StartGameFromMatchmakingAsync

public Task<MatchmakingStartResponse> StartGameFromMatchmakingAsync(string playerToken, CancellationToken ct = default)

Starts a game from matchmaking lobby. Transfers all players to a new game room.

StopMatchmakingAsync

public Task<SuccessResponse> StopMatchmakingAsync(string playerToken, CancellationToken ct = default)

Stops matchmaking lobby (Host Only). Cannot be called after game has started.

KickPlayerAsync

public Task<MatchmakingKickResponse> KickPlayerAsync(string playerToken, int playerId, CancellationToken ct = default)

Kicks a player from matchmaking lobby (Host Only). Cannot kick yourself or after matchmaking has started.

UpdateMatchmakingPasswordAsync

public Task<SuccessResponse> UpdateMatchmakingPasswordAsync(string playerToken, string password, CancellationToken ct = default)

Updates the matchmaking password (Host Only). Cannot change password after matchmaking has started. Use empty string to remove password.

Leaderboard

Methods for competitive rankings and player statistics

GetLeaderboardAsync

public Task<LeaderboardResponse<T>> GetLeaderboardAsync<T>(string[] sortBy, int limit = 10, CancellationToken ct = default)

Retrieves ranked players with configurable sorting criteria, limit (1-100), and typed player data support.