// User provides their code (from actioncode.app)const code = '48291037' // example code// Resolve it to verify and get detailsconst actionCode = await client.resolve(code)console.log('Valid code from wallet:', actionCode.pubkey)console.log('Expires at:', actionCode.expiresAt)
Now attach what you want the user to approve. Choose based on your use case:
Sign a Transaction
Sign a Message
import { Transaction, SystemProgram, PublicKey, Connection } from '@solana/web3.js'// Build your transactionconst connection = new Connection('https://api.mainnet-beta.solana.com')const { blockhash } = await connection.getLatestBlockhash()const transaction = new Transaction({ recentBlockhash: blockhash, feePayer: new PublicKey(actionCode.pubkey)}).add( SystemProgram.transfer({ fromPubkey: new PublicKey(actionCode.pubkey), toPubkey: new PublicKey('RecipientAddressHere'), lamports: 1_000_000 // 0.001 SOL }))// Serialize and attach to the codeconst serialized = transaction.serialize({ requireAllSignatures: false}).toString('base64')await client.attachTransaction(code, serialized)
// Create a message for the user to signconst message = 'Sign in to MyApp - ' + new Date().toISOString()// Attach it to the codeawait client.attachMessage(code, message)
The user will see the pending request in actioncode.app and can approve it with their wallet.
// Watch for the user to approvefor await (const status of client.observeStatus(code)) { console.log('Current status:', status.status) // Check for transaction signature if (status.finalizedSignature) { console.log('Transaction signed!') console.log('Signature:', status.finalizedSignature) break } // Check for signed message if (status.signedMessage) { console.log('Message signed!') console.log('Signed message:', status.signedMessage) break }}
Here’s everything together — a simple flow where a user signs a message:
import { ActionCodesClient } from '@actioncodes/sdk'const client = new ActionCodesClient({ authToken: process.env.ACTION_CODES_TOKEN})async function requestSignature(userCode: string) { // 1. Verify the code const actionCode = await client.resolve(userCode) console.log(`Code valid for wallet: ${actionCode.pubkey}`) // 2. Attach a message to sign const message = `Verify ownership of ${actionCode.pubkey} at ${Date.now()}` await client.attachMessage(userCode, message) console.log('Message attached — waiting for user approval...') // 3. Wait for signature for await (const status of client.observeStatus(userCode)) { if (status.signedMessage) { console.log('Success! Signed message:', status.signedMessage) return status.signedMessage } // Handle expiry if (status.status === 'expired') { throw new Error('Code expired before approval') } }}// Usageconst userCode = '48291037' // User provides this from actioncode.appconst signedMessage = await requestSignature(userCode)