@agenticwallet/js
v0.1.0
Published
AgentWallet TypeScript/React SDK
Readme
@agenticwallet/js
TypeScript/React SDK for AgentWallet — a metered proxy to the Anthropic API with multi-tenant support, JWT auth, and credit-based billing.
Install
npm install @agenticwallet/jsBrowser Usage
AgentWalletProvider
Wrap your app with the provider to access wallet functionality:
import { AgentWalletClient, AgentWalletProvider } from '@agenticwallet/js';
const client = new AgentWalletClient('https://wallet.example.com', token);
export function App() {
return (
<AgentWalletProvider client={client}>
<YourApp />
</AgentWalletProvider>
);
}BalanceBadge
Display the current balance. Refreshes automatically on window focus:
import { BalanceBadge } from '@agenticwallet/js';
export function Header() {
return <BalanceBadge />;
}BuyCreditsButton
Open Stripe Checkout for credit purchases. Balance updates automatically after redirect:
import { BuyCreditsButton } from '@agenticwallet/js';
export function CreditShop() {
return (
<BuyCreditsButton
pack={{ price_cents: 9999, points: 1000 }}
successUrl={window.location.href}
cancelUrl={window.location.href}
/>
);
}createWalletedAnthropic
Wrap the Anthropic SDK to route messages through AgentWallet:
import { createWalletedAnthropic, useAgentWallet } from '@agenticwallet/js';
export function ChatComponent() {
const { client } = useAgentWallet();
const anthropic = createWalletedAnthropic(client);
const handleSend = async (message: string) => {
const response = await anthropic.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 1024,
messages: [{ role: 'user', content: message }],
});
// Balance updates after response is processed
};
return <div>Chat UI</div>;
}Node.js Usage
The Node.js build includes mintUserJwt for server-side token generation (e.g., Firebase Cloud Functions):
import { mintUserJwt } from '@agenticwallet/js';
export const mintAgentWalletToken = async (sharedSecret: string, tenantId: string, userId: string) => {
const token = mintUserJwt(sharedSecret, tenantId, userId, 900); // 15-min TTL
return { token };
};Firebase Functions example:
import * as functions from 'firebase-functions';
import { mintUserJwt } from '@agenticwallet/js';
export const mintAgentWalletToken = functions.https.onCall(async (data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('unauthenticated', 'Must be authenticated');
}
const sharedSecret = functions.config().agentwallet.shared_secret;
const tenantId = functions.config().agentwallet.tenant_id;
const token = mintUserJwt(sharedSecret, tenantId, context.auth.uid, 900);
return { token };
});API Reference
AgentWalletClient(baseUrl, token)— Create a client. Update token viaclient.setToken(newToken)for JWT refresh.AgentWalletProvider— React context provider. ExposesuseAgentWallet()hook.BalanceBadge— Component displaying current balance. RequiresAgentWalletProvider.BuyCreditsButton— Component for Stripe Checkout. RequiresAgentWalletProvider.createWalletedAnthropic(client)— Returns an Anthropic SDK instance routed through AgentWallet.mintUserJwt(sharedSecret, tenantId, userId, ttlSeconds?)— Generate HS256 JWT (Node only).
More Info
See the AgentWallet service documentation for complete architecture and integration guides.
