This is a guide on how you can integrate Session Keys into your Solana Anchor programs.


cargo add session-keys --features no-entrypoint


  1. Import the dependencies.
use session_keys::{SessionError, SessionToken, session_auth_or, Session};
  1. Derive the Session trait on your instruction struct.
#[derive(Accounts, Session)]
pub struct Instruction<'info> {
    pub user: Account<'info, User>,

        // The ephemeral keypair signing the transaction
        signer = signer,
        // The authority of the user account which must have created the session
        authority = user.authority.key()
    // Session Tokens are passed as optional accounts
    pub session_token: Option<Account<'info, SessionToken>>,

    pub signer: Signer<'info>,
  1. Add the session_auth_or macro to your instruction handler with fallback logic on who the instruction should validate the signer when sessions are not present and an appropriate ErrorCode.

    If you’ve used require*! macros in anchor_lang, you already know how this works.
    ctx.accounts.user.authority.key() == ctx.accounts.authority.key(),
pub fn ix_handler(ctx: Context<Instruction>,) -> Result<()> {

Was this page helpful?