Quick Reference for AI Agents & Developers
Available via: SDK | REST API | CometChat Dashboard
Prerequisites
Before using Token Based File 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 for details.
Secure Media Modes
CometChat supports three modes for accessing media files on its secure media servers. The mode is configured during SDK initialization usingAppSettingsBuilder.secureMediaMode.
Embedded Mode (Default)
Embedded Mode (SecureMediaMode.embedded) 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
Image.network(), video players, and other media widgets
Header-Based Mode
Header-Based Mode (SecureMediaMode.headerBased) provides media access by adding the File Access Token (FAT) in the request headers. In this mode, the SDK adds the FAT to the Authorization header when making media requests.
- Media URLs remain clean without query parameters
- The FAT is sent in the request header instead of the URL
- Requires custom HTTP client configuration to include the FAT header
- Ideal for applications that need clean URLs but can handle custom headers
Presigned Mode
Presigned Mode (SecureMediaMode.presigned) 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 or headers
- 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
TheSecureMediaMode enum defines the available secure media access modes:
| Parameter | Value | Description |
|---|---|---|
SecureMediaMode.embedded | 0 | Default mode. FAT is appended as a ?fat=<token> query parameter to media URLs automatically by the SDK. |
SecureMediaMode.headerBased | 1 | Header mode. FAT is added to the Authorization header when making media requests. |
SecureMediaMode.presigned | 2 | Presigned 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.secureMediaMode. Pass a value from the SecureMediaMode enum to select your preferred mode.
- Embedded Mode
- Header-Based Mode
- Presigned Mode
If you omit
secureMediaMode, 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 Presigned Mode or 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.
- Callback
- 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 |
|---|---|
Future<String> | Resolves with a time-limited presigned URL that grants temporary access to the media file. |
How Attachment URLs Differ
When Token Based File 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 with
Image.network()or video players - 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, the FAT is sent in the request header instead of the URL. You need to configure your HTTP client to include the FAT in theAuthorization header when making media requests.
- URLs remain clean without query parameters
- Use
CometChat.getFat()to get the current FAT and add it to your request headers - Requires custom HTTP client configuration for media loading
Presigned Mode
In Presigned 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, add the FAT to request headers. In Presigned Mode, always resolve the URL with
CometChat.fetchPresignedUrl() before passing it to an image or media widget.API Reference
The following static members are available on theCometChat class for working with Token Based File Access:
| Member | Type | Description |
|---|---|---|
SecureMediaMode | enum | Enum with three values: embedded (0), headerBased (1), and presigned (2). Used with AppSettingsBuilder.secureMediaMode to configure the secure media access mode. |
CometChat.getFat() | String? | Returns the decoded File Access Token (FAT) for the current session, or null if no valid FAT is available. |
CometChat.fetchPresignedUrl(url) | Future<String> | Resolves a time-limited presigned URL for the given secure media URL. Used in Presigned 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 headerBased, a valid FAT exists for the current session, and the secure media host is configured.
- Dart
| Parameter | Type | Description |
|---|---|---|
| — | — | This method takes no parameters. |
| Return Type | Description |
|---|---|
bool | true if the mode is headerBased, 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 special handling for secure access.
- Callback
- 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 |
|---|---|
bool | true if the URL points to the configured secure media host. false otherwise. |
Error Handling
When working with Token Based File Access, handle these common error scenarios gracefully in your application.Feature Not Enabled
If Token Based File 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.
- Callback
- 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.
- Callback
- 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.- Callback
- 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 the Right Mode
Choose the Right Mode
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 with
Image.network().Use Header-Based Mode when you need clean URLs but can configure custom HTTP headers. This mode is suitable for applications using custom HTTP clients or CDN proxies that can forward headers.Use Presigned Mode when you need clean URLs, custom caching strategies, or full control over how media files are fetched. This mode is better suited for applications with custom media players 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 Presigned Mode.Solution:- Check the configured mode using
CometChatHelper.isHeaderModeEnabled(). - Verify the
secureMediaModevalue passed during SDK initialization. - In Embedded Mode (
SecureMediaMode.embedded), URLs automatically include?fat=<token>— do not callfetchPresignedUrl(). - In Header-Based Mode (
SecureMediaMode.headerBased), add the FAT to request headers. - In Presigned Mode (
SecureMediaMode.presigned), URLs are clean — always callfetchPresignedUrl()before rendering.