@socketfi/server package, which validates the cryptographic signature on every access token and returns the verified user identity and wallet address.
Installation
Install the SocketFi Server SDK in your backend project:Verifying a Token
ImportverifyAuth and pass it the raw JWT string extracted from the Authorization: Bearer header:
verifyAuth is asynchronous. It validates the token’s cryptographic signature, checks all standard JWT claims (expiration, issuer, audience), and returns a typed result object.
Verification Results
verifyAuth always returns one of two shapes — a successful result or a failure result. Check result.valid before accessing any other fields.
Successful Verification
Failed Verification
Error Codes
Express Middleware
The cleanest way to protect your Express routes is an authentication middleware that runsverifyAuth before your route handlers:
Extend the
express.Request type to include socketfi so TypeScript recognizes the property in downstream handlers:Extracting User Identity and Wallet Address
Once verification succeeds,result.user.id and result.wallet.address are the authoritative identifiers for the requesting user. Use them to look up records, authorize operations, and attribute on-chain activity.
Full Verification Example
Here is a complete, self-contained example that covers token extraction, verification, error handling, and identity usage:Development and Testing Utilities
The@socketfi/server package exposes one additional utility that is useful during local development and testing.
clearKeyCache()
verifyAuth() caches the public signing keys it fetches from SocketFi to avoid redundant network requests on every verification call. In production this cache is managed automatically and you never need to touch it.
During local development or in test suites — for example, when rotating keys in a staging environment or writing unit tests that stub the verification flow — you can flush the cache manually:
clearKeyCache() is a development and testing utility. Production applications should never need to call it — the SDK manages key caching automatically and refreshes keys when necessary.Security Checklist
Before shipping to production, confirm that your backend satisfies all of these requirements:1
Call verifyAuth on every protected endpoint
No exceptions. Every API route that returns user data, reads wallet state, or performs any authenticated action must verify the token before proceeding.
2
Return 401 for all verification failures
Do not leak information about why a token failed. Return
401 Unauthorized for every result.valid === false case regardless of the error code. You can log the error code internally for debugging.3
Use result.user.id as your identity source of truth
Never trust a user ID or wallet address submitted in a request body or query parameter. The only authoritative identity is what
verifyAuth returns.4
Log INVALID_SIGNATURE and INVALID_ISSUER errors
These codes indicate potential token forgery or a misconfigured client. Alert your security monitoring system when they appear.
5
Run your server over HTTPS
Tokens transmitted over plain HTTP can be intercepted. All production endpoints must use TLS.