Alt description missing in image
Beta: Plugins coming soon!
@auth/clerkutilsgetMobileAuthHeaders

getMobileAuthHeaders()

import { getMobileAuthHeaders } from '@auth/clerk'
        • getMobileAuthHeaders()

On mobile (Expo), cookies are not available for authenticating API calls. This helper retrieves the Clerk session token and returns the proper Authorization header so your fetch calls are authenticated against your Next.js backend.

On web, this function is a no-op and returns an empty object because cookies are used automatically.

Usage

fetchers.ts
import { getMobileAuthHeaders } from '@auth/clerk'
 
export async function fetchProtectedResource() {
    const authHeaders = await getMobileAuthHeaders()
    const res = await fetch('https://your-api-endpoint.com/protected', {
        method: 'GET',
        headers: {
            ...authHeaders,
            'Content-Type': 'application/json',
        },
    })
    if (!res.ok) throw new Error('Failed to fetch')
    return res.json()
}

API

Signature

type AuthHeaders = { Authorization: string }
export declare function getMobileAuthHeaders(): Promise<Partial<AuthHeaders>>

Behavior

  • On Expo (native): returns { Authorization: 'Bearer <token>' } when a session token exists, otherwise {}.
  • On Web: returns {}.
For cross-platform data fetching, this util is already integrated into our GraphQL helpers as described in the plugin README.