import { createAgentClient } from '@ledewire/node'
const agent = createAgentClient({
key: process.env.LEDEWIRE_BUYER_KEY,
secret: process.env.LEDEWIRE_BUYER_SECRET,
})
// The first call logs in with the key; no explicit login is needed
const balance = await agent.wallet.balance()
// Check access before buying: a preview and access_info, never the deliverable
const preview = await agent.content.getWithAccess('abc123')
if (preview.access_info.next_required_action === 'purchase') {
// The purchase response is the only delivery (single-use): keep it now
const purchase = await agent.purchases.create({
content_id: preview.id,
price_cents: preview.price_cents,
})
const deliverable = purchase.content_body ?? purchase.content_uri
}
Creates a LedeWire client optimised for autonomous agents.
The client logs in by itself with the buyer API key and secret: the first authenticated call exchanges them for a session (through
auth.loginWithBuyerApiKey()), concurrent first calls share that one login, and theTokenManagerrefreshes the session in the background. If a refresh fails, the client logs in again with the key and retries the call once. No explicitauth.loginWithBuyerApiKey()call is needed. A wrong or revoked key rejects the call with the API'sAuthErrorafter a single login attempt.A client that already holds a token (from
storage, or an explicit login) does not log in. Don't mix another identity on an agent client (another buyer key, orstorageholding someone else's session): if that session's refresh fails, the client falls back to the configured key.Only buyer-relevant namespaces are exposed.
merchantandconfigare intentionally absent.