API ReferenceReact provider and hooksFunction
useAnvilSDK()
function useAnvilSDK(): AnvilSDK;Returns the AnvilSDK instance from context — the one passed to <AnvilProvider sdk={...}> — so
sdk.loc, sdk.vault, sdk.pricing, and sdk.tokens are reachable from any component beneath it.
Throws if called outside of <AnvilProvider>; use useAnvilSDKOptional for
components that may render without one.
Returns
The provider’s SDK instance. Never null: a provider carrying a null SDK is treated as absent and
throws, so a component that reaches past this line has a usable SDK.
Throws
Error when no <AnvilProvider> is above this component, or when the nearest one still carries
sdk={null}.
Example
export function App() { // Build the SDK once (module scope or useMemo); hooks key on it. return ( <AnvilProvider sdk={sdk}> <LOCBeneficiary id={1n} /> </AnvilProvider> );}
function LOCBeneficiary({ id }: { id: bigint }) { // Throws if this component renders outside <AnvilProvider>. const sdk = useAnvilSDK(); const [loc, setLoc] = useState<OutstandingLetterOfCredit>();
useEffect(() => { void sdk.loc.getOutstandingLetterOfCredit({ id }).then(setLoc); }, [sdk, id]);
return <p>Beneficiary: {loc?.beneficiary ?? 'loading'}</p>;}