Skip to content
LogoLogo

Sign Zone Authorization Token

zone.signAuthorizationToken

Signs a zone authorization token and stores it for the zone HTTP transport. The token authenticates subsequent zone RPC requests via the X-Authorization-Token header.

Usage

import {  } from './viem.config'
 
const {  } = await ..()
 
.('Token:', )
Token: 0x...

Recipes

Issue a Short-Lived Token for a Backend Job

Pass expiresAt to bound the token's validity to the job's runtime, limiting exposure if the token leaks from a worker environment.

import {  } from './viem.config'
 
const  = .(.() / 1000)
 
const {  } = await ..({
  :  + 15 * 60,
})

Share One Token Across Processes

Pass a custom storage backed by a shared store, so a fleet of workers reuses a single signed token instead of each process re-signing.

import {  } from 'viem/tempo'
import {  } from './viem.config'
 
// Swap the Map for a shared store such as Redis.
const  = new <string, string>()
 
const  = .({
  : () => .() ?? null,
  : (, ) => {
    .(, )
  },
  : () => {
    .()
  },
})
 
const {  } = await ..({
  ,
})

Return Value

type ReturnType = {
  /** Authentication object. */
  authentication: ZoneRpcAuthentication
  /** Serialized authorization token. */
  token: Hex
}

The signed authentication object and the serialized token.

Parameters

account

  • Type: Account | Address
  • Default: client.account

Account to sign the authorization token with. Must be a local account.

chain

  • Type: Chain
  • Default: client.chain

Zone chain to sign the token for.

expiresAt

  • Type: number
  • Default: issuedAt + 86_400

Unix timestamp when the token expires.

issuedAt

  • Type: number
  • Default: Math.floor(Date.now() / 1000)

Unix timestamp when the token was issued.

storage

  • Type: Storage
  • Default: Storage.defaultStorage()

Storage instance to persist the signed token.