Documentation Index
Fetch the complete documentation index at: https://mintlify.com/matrix-org/matrix-js-sdk/llms.txt
Use this file to discover all available pages before exploring further.
Overview
MatrixClient is the main class in matrix-js-sdk. It wraps the Matrix Client-Server API and provides a high-level interface for authentication, room operations, messaging, media handling, and real-time sync. It extends TypedEventEmitter, so you can listen for events using .on(), .once(), and .off().
You should not instantiate MatrixClient directly. Use createClient() instead.
Client Lifecycle
startClient(opts?)
Start syncing with the homeserver. Must be called before most other methods will return live data.
Configuration for the sync loop.
Resolves once the sync loop has started.
stopClient()
Stop syncing and perform a clean shutdown of the client.
clearStores(args?)
Delete all data from the persistent stores used by this client. The client must not be running when this is called.
The IndexedDB database name prefix to clear. Defaults to
'matrix-js-sdk'.Resolves when all stores have been cleared.
Room Operations
createRoom(options)
Create a new Matrix room.
Room creation options passed directly to the
/createRoom API. Common fields include name, topic, visibility ("public" | "private"), preset, and invite.The ID of the newly created room.
joinRoom(roomIdOrAlias, opts?)
Join a room by its ID or alias. No-ops if already joined.
The room ID (
!abc:example.com) or alias (#general:example.com) to join.Optional join parameters, such as
viaServers (array of server names to route the request through).The
Room object for the joined room.leave(roomId)
Leave a room.
The ID of the room to leave.
Resolves to an empty object on success.
getRoom(roomId)
Retrieve a room from the local store.
The ID of the room to retrieve.
The
Room object if found, or null if not in the store.getRooms()
Retrieve all rooms from the local store.
All rooms currently in the store, or an empty array if there is no store.
publicRooms(options?)
Query the homeserver’s public room directory.
Filtering options. Includes
server, limit, since, and filter fields like generic_search_term.Contains
chunk (array of public room summaries), next_batch, and prev_batch.Messaging
sendEvent(roomId, eventType, content, txnId?)
Send a Matrix timeline event to a room.
The ID of the room to send the event to.
The event ID of the thread root. Pass
null for the main timeline.The Matrix event type, e.g.
"m.room.message".The event content object.
Optional transaction ID to deduplicate repeated attempts.
The ID assigned to the sent event by the homeserver.
sendMessage(roomId, content, txnId?)
Send an m.room.message event.
sendTextMessage(roomId, body, txnId?)
Convenience wrapper around sendMessage for plain-text messages.
The room to send the message to.
The plain-text message body.
Optional transaction ID.
redactEvent(roomId, eventId, txnId?, opts?)
Redact (soft-delete) an event in a room.
The ID of the room containing the event.
The ID of the event to redact.
A human-readable reason for the redaction.
Membership
invite(roomId, userId, opts?)
Invite a user to a room.
The room to invite the user to.
The MXID of the user to invite.
Options, or a plain string treated as a
reason. opts.shareEncryptedHistory will share room key history if the room’s visibility allows it.kick(roomId, userId, reason?)
Kick a user from a room.
ban(roomId, userId, reason?)
Ban a user from a room.
unban(roomId, userId)
Lift a ban from a user in a room.
User Profile
setDisplayName(name)
Set the display name of the logged-in user.
setAvatarUrl(url)
Set the avatar URL of the logged-in user.
An MXC URL pointing to the avatar image (obtained via
uploadContent).getProfileInfo(userId, info?)
Fetch profile information for any user.
The MXID of the user whose profile to fetch.
Retrieve a specific field only:
"displayname" or "avatar_url".Presence
setPresence(opts)
Set the presence status of the logged-in user via the Presence API.
One of
"online", "offline", or "unavailable".A human-readable status message.
Account Data
setAccountData(eventType, content)
Set user-scoped account data and wait for the echo to be received over /sync.
The account data event type, e.g.
"m.direct".The content to store.
getAccountData(eventType)
Read a user-scoped account data event from the local store.
Media
uploadContent(file, opts?)
Upload a file to the homeserver’s media repository.
In browsers, a
File or Blob. In Node.js, a Buffer, string, or ReadStream.Upload options. Includes
name (filename), type (MIME type), progressHandler, and includeFilename.The MXC URI of the uploaded content, e.g.
mxc://matrix.org/abc123.mxcUrlToHttp(mxcUrl, width?, height?, resizeMethod?, ...)
Convert an MXC URI to an HTTP URL.
The
mxc:// URI to convert.Desired thumbnail width in pixels.
Desired thumbnail height in pixels.
Thumbnail resize method:
"crop" or "scale".If
true, return non-MXC URLs as-is. This may leak information. Default: false.If
true, the caller accepts 307/308 redirects. Default: false.If
true, return an authenticated media URL. Implies allowRedirects. Default: false.The HTTP URL, or
null if the MXC URL is invalid.Authentication
login(loginType, data)
Post a login request. Updates the client with returned credentials.
The login flow type, e.g.
"m.login.password" or "m.login.token".Flow-specific credentials, e.g.
{ user, password } for m.login.password.logout(stopClient?)
Post a logout request to the homeserver, invalidating the current access token.
If
true, also call stopClient() and abort all pending HTTP requests before logging out. Default: false.getAccessToken()
Return the current access token.
getUserId()
Return the MXID of the logged-in user.
getDeviceId()
Return the device ID of this client.
Server Info
getVersions()
Query the server’s supported spec versions. Results are cached.
Array of supported Matrix spec versions, e.g.
["v1.6", "v1.7"].Map of unstable feature flags.
getCapabilities()
Query the homeserver’s capabilities (e.g. supported room versions, password change support).
An object describing server capabilities. See
Capabilities in serverCapabilities.ts.