Quick Reference for AI Agents & Developers
Available via: SDK | REST API | CometChat Dashboard
Prerequisites
Before using Secure Media Access in your application, ensure the following:- Enable in Dashboard: Navigate to your CometChat Dashboard → Chats → Settings and enable the Token Based File Access configuration. This is a one-time setup step that activates token-based media access for your app.
- SDK Initialization: Your application must initialize the CometChat SDK with a valid App ID and region before configuring secure media modes. See Setup SDK for details.
Secure Media Modes
CometChat supports two modes for accessing media files on its secure media servers. The mode is configured during SDK initialization usingAppSettingsBuilder.setSecureMediaMode().
Embedded Mode (Default)
Embedded Mode (SecureMediaMode.EMBEDDED = 0) is the default mode. In this mode, the SDK automatically appends the File Access Token (FAT) as a ?fat=<token> query parameter to all media URLs. No additional work is required from the developer — media URLs are ready to use as-is.
- The SDK handles FAT injection transparently
- Media URLs include the token directly:
https://files-<region>.cometchat.com/<app_id>/media/file.jpg?fat=eyJhbGci... - Suitable for simple integrations where clean URLs are not a concern
- Works out of the box with
<img>,<video>, and<audio>HTML elements
Header-Based Mode
Header-Based Mode (SecureMediaMode.HEADER_BASED = 1) provides clean media URLs without embedded tokens. In this mode, the developer is responsible for calling CometChat.fetchPresignedUrl() to resolve a time-limited presigned URL before rendering media.
- Media URLs remain clean without query parameters
- Developer calls
CometChat.fetchPresignedUrl(url)to get a presigned URL at render time - Presigned URLs are time-limited and grant temporary access to the media file
- Ideal for applications that need more security, clean URLs or custom media handling logic
SecureMediaMode Enum
TheCometChat.SecureMediaMode enum defines the available secure media access modes:
| Parameter | Type | Description |
|---|---|---|
CometChat.SecureMediaMode.EMBEDDED | 0 | Default mode. FAT is appended as a ?fat=<token> query parameter to media URLs automatically by the SDK. |
CometChat.SecureMediaMode.HEADER_BASED | 1 | Clean URL mode. Developer calls CometChat.fetchPresignedUrl() to resolve a time-limited presigned URL for media access. |
Configuration
Configure the secure media mode during SDK initialization usingAppSettingsBuilder.setSecureMediaMode(). Pass a value from the CometChat.SecureMediaMode enum to select your preferred mode.
- JavaScript
- TypeScript
- Async/Await
If you omit
setSecureMediaMode(), the SDK defaults to Embedded Mode (SecureMediaMode.EMBEDDED). In Embedded Mode, the SDK automatically appends the FAT as a query parameter to media URLs — no additional configuration is needed.Fetching Presigned URLs
When using Header-Based Mode, callCometChat.fetchPresignedUrl() to resolve a time-limited presigned URL for any secure media file. The presigned URL grants temporary access to the media file without requiring additional authentication headers.
- JavaScript
- TypeScript
- Async/Await
Parameters
| Parameter | Type | Description |
|---|---|---|
url | string | The secure media URL to resolve. Must point to a file hosted on CometChat’s secure media servers. |
Return Value
| Type | Description |
|---|---|
Promise<string> | Resolves with a time-limited presigned URL that grants temporary access to the media file. |
How Attachment URLs Differ
When Secure Media Access is enabled, the format of media attachment URLs depends on the configured mode. Understanding this difference is important for rendering media correctly in your application.Embedded Mode
In Embedded Mode, the SDK automatically appends the File Access Token (FAT) as a?fat=<token> query parameter to every media URL. Attachment URLs returned by the SDK are ready to use directly — no additional processing is needed.
- URLs are self-contained and can be used directly in
<img>,<video>, or<audio>elements - The SDK handles token injection transparently when you access attachment URLs from message objects
- No developer action is required at render time
Header-Based Mode
In Header-Based Mode, attachment URLs remain clean without any token parameters. The developer must explicitly callCometChat.fetchPresignedUrl() at render time to resolve a time-limited presigned URL before displaying the media.
- URLs do not contain any token information
- Before rendering, call
CometChat.fetchPresignedUrl(url)to get a presigned URL - The presigned URL is time-limited — fetch a new one if it expires
- Gives you full control over when and how media files are loaded
In Embedded Mode, you can use attachment URLs directly from message objects. In Header-Based Mode, always resolve the URL with
CometChat.fetchPresignedUrl() before passing it to an image or media element.API Reference
The following static members are available on theCometChat class for working with Secure Media Access:
| Member | Type | Description |
|---|---|---|
CometChat.SecureMediaMode | enum | Enum with two values: EMBEDDED (0) and HEADER_BASED (1). Used with AppSettingsBuilder.setSecureMediaMode() to configure the secure media access mode. |
CometChat.getFat() | () => string | null | Returns the decoded File Access Token (FAT) for the current session, or null if no valid FAT is available. |
CometChat.fetchPresignedUrl(url) | (url: string) => Promise<string> | Resolves a time-limited presigned URL for the given secure media URL. Used in Header-Based Mode to get a temporary URL that grants access to the media file. |
Helper Methods
TheCometChatHelper class provides utility methods to check the current secure media configuration at runtime. These are synchronous methods that return values directly.
isHeaderModeEnabled()
Returnstrue when all three conditions are met: the secure media mode is set to HEADER_BASED, a valid FAT exists for the current session, and the secure media host is configured.
- JavaScript
- TypeScript
- Async/Await
| Parameter | Type | Description |
|---|---|---|
| — | — | This method takes no parameters. |
| Return Type | Description |
|---|---|
boolean | true if the mode is HEADER_BASED, a valid FAT exists, and the secure media host is configured. false otherwise. |
requiresSecureAccess(url)
Returnstrue if the given URL points to the configured secure media host. Use this to determine whether a URL needs to be resolved via CometChat.fetchPresignedUrl() before rendering.
- JavaScript
- TypeScript
- Async/Await
| Parameter | Type | Description |
|---|---|---|
url | string | The media URL to check. The method compares the URL’s host against the configured secure media host. |
| Return Type | Description |
|---|---|
boolean | true if the URL points to the configured secure media host. false otherwise. |
Error Handling
When working with Secure Media Access, handle these common error scenarios gracefully in your application.Feature Not Enabled
If Secure Media Access is not enabled in the CometChat Dashboard, callingCometChat.fetchPresignedUrl() will not throw an error — it simply returns the same URL string that was passed in. Media URLs work normally without a File Access Token in this case.
- JavaScript
- TypeScript
- Async/Await
Invalid URL Provided
If an invalid or malformed URL is passed toCometChat.fetchPresignedUrl(), the method does not throw an error — it returns the same string that was passed in.
- JavaScript
- TypeScript
- Async/Await
Expired File Access Token (FAT)
When the File Access Token expires, media requests return a 401 Unauthorized response. The SDK automatically refreshes the FAT on re-login. In your application, handle 401 responses by re-fetching the presigned URL.- JavaScript
- TypeScript
- Async/Await
Best Practices
Cache Presigned URLs
Cache Presigned URLs
Presigned URLs are time-limited but valid for a reasonable duration. Cache them in memory to avoid redundant network calls when rendering the same media file multiple times (e.g., in a scrolling message list). Invalidate the cache when the URL expires or returns a 401 response.
Refresh Expired URLs
Refresh Expired URLs
Presigned URLs expire after a set duration. If a media request returns a 401 or 403 response, discard the cached URL and fetch a new presigned URL. Implement a retry mechanism that re-fetches the presigned URL once before showing an error to the user.
Handle 401 Responses Gracefully
Handle 401 Responses Gracefully
A 401 Unauthorized response on a media URL means the File Access Token (FAT) is missing, expired, or invalid. In your application, catch 401 errors and attempt to re-fetch the presigned URL. If the error persists, prompt the user to re-authenticate — the SDK refreshes the FAT automatically on login.
Choose Between Embedded and Header-Based Modes
Choose Between Embedded and Header-Based Modes
Use Embedded Mode when you want zero-effort media rendering — the SDK appends the FAT to every media URL automatically. This is ideal for simple chat UIs where you display media directly in
<img> or <video> elements.Use Header-Based Mode when you need clean URLs, custom caching strategies, or control over how media files are fetched. This mode is better suited for applications with custom media players, CDN proxies, or advanced image loading pipelines.You can check the current mode at runtime using CometChatHelper.isHeaderModeEnabled().Troubleshooting
401 Unauthorized on Media URLs
401 Unauthorized on Media URLs
Expired Presigned URLs
Expired Presigned URLs
Symptom: A presigned URL that previously worked now returns a 401 or 403 response.Cause: Presigned URLs are time-limited. Once the URL expires, it can no longer be used to access the media file.Solution:
- Discard the expired presigned URL from any local cache.
- Call
CometChat.fetchPresignedUrl(url)again with the original media URL to get a fresh presigned URL. - Implement a retry mechanism in your media loading logic to handle expired URLs automatically.
Mode Mismatch Between SDK and Media URLs
Mode Mismatch Between SDK and Media URLs
Symptom: Media URLs contain
?fat=<token> when you expect clean URLs, or vice versa.Cause: The SDK is configured in a different mode than expected. For example, the SDK is in Embedded Mode but your code assumes Header-Based Mode.Solution:- Check the configured mode using
CometChatHelper.isHeaderModeEnabled(). - Verify the
setSecureMediaMode()value passed during SDK initialization. - In Embedded Mode (
SecureMediaMode.EMBEDDED), URLs automatically include?fat=<token>— do not callfetchPresignedUrl(). - In Header-Based Mode (
SecureMediaMode.HEADER_BASED), URLs are clean — always callfetchPresignedUrl()before rendering.