Matrix stores all media — images, videos, files, avatars — on the homeserver’s media repository. Each uploaded file is assigned anDocumentation 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.
mxc:// URI that clients use to reference it. The SDK provides helpers to upload content and convert MXC URIs to HTTP download URLs.
Uploading media
Useclient.uploadContent() to upload a file to the homeserver:
mxcUri in a message event:
Converting MXC URIs to HTTP URLs
Useclient.mxcUrlToHttp() to convert an mxc:// URI to a usable HTTP URL:
Thumbnail URLs
Passwidth, height, and an optional resizeMethod to request a server-generated thumbnail:
getHttpUriForMxc() function shows how the URL is constructed:
- If
width,height, orresizeMethodare set the URL points to/_matrix/media/v3/thumbnail/.... - Otherwise the URL points to
/_matrix/media/v3/download/.... - When
useAuthenticationistruethe URL uses the authenticated endpoint/_matrix/client/v1/media/...andallowRedirectsis automatically set totrue(required by MSC3916).
Authenticated media (MSC3916 / Matrix 1.11)
Matrix 1.11 introduced MSC3916, which requires clients to supply anAuthorization header when accessing media. Servers that have enabled this feature will reject unauthenticated media requests.
Generating an authenticated URL
SetuseAuthentication: true to get an authenticated endpoint URL:
Making the authenticated request
In Node.js, or wherever you control the fetch call, append theAuthorization header:
Authenticated thumbnails
Authenticated media works with thumbnails too:Service Workers for browser authenticated media
In browsers, HTML elements such as<img> and <video> do not allow you to set request headers. To use authenticated media in these contexts you need a Service Worker that intercepts requests and appends the Authorization header automatically.
Element Web uses this pattern via the
matrix-content-scanner and a custom Service Worker. The matrix-js-sdk itself does not bundle a Service Worker — you must implement one appropriate for your application’s authentication model.Animated media
Passanimated: true to request an animated thumbnail (e.g. animated GIF or WebP) if the server supports it:
MXC URI validation
The SDK validates MXC URIs before constructing URLs. An invalid MXC (malformed server name, invalid media ID characters, extra path segments) will return an empty string:Parameter reference
mxcUrlToHttp() / getHttpUriForMxc() parameters
mxcUrlToHttp() / getHttpUriForMxc() parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
mxc | string | — | The mxc:// URI to resolve |
width | number | undefined | Desired thumbnail width in pixels |
height | number | undefined | Desired thumbnail height in pixels |
resizeMethod | string | undefined | "scale" or "crop" |
allowDirectLinks | boolean | false | Return non-mxc:// URLs as-is (leaks info) |
allowRedirects | boolean | undefined | Indicates the caller supports HTTP redirects |
useAuthentication | boolean | false | Use the authenticated /_matrix/client/v1/media/ endpoint |
animated | boolean | undefined | Request an animated thumbnail |