// Borrou Partner API: the TypeScript snippets, run against the sandbox. v0.1 DRAFT. // // export BORROU_BASE_URL=https://api.borrou.capital // export BORROU_CLIENT_ID=bpc_test_... # your sandbox credential // export BORROU_CLIENT_SECRET=... # from your secret store, never from a file you commit // npx tsx quickstart.ts // // Reads your products, a quote, the first page of your events and every one of your loans, and // submits one application for a synthetic borrower: an idempotent POST, sent twice with the same key // to show the replay. Writes run with a sandbox credential only. import { randomUUID } from "node:crypto"; import { BorrouApiError, BorrouClient } from "./borrouClient"; function required(name: string): string { const value = process.env[name]; if (!value) throw new Error(`Set ${name}.`); return value; } interface Product { code: string; api_lendable: boolean; limits: { min_principal_usdt: string; max_principal_usdt: string }; } async function main() { const clientId = required("BORROU_CLIENT_ID"); const client = new BorrouClient({ baseUrl: process.env.BORROU_BASE_URL ?? "https://api.borrou.capital", clientId, clientSecret: required("BORROU_CLIENT_SECRET") }); const products = await client.get<{ data: Product[] }>("/v1/products"); const product = products.body.data.find((p) => p.api_lendable); if (!product) throw new Error("No product can lend through the API yet: ask TFG to activate one."); console.log(`Product ${product.code}, ${product.limits.min_principal_usdt} to ${product.limits.max_principal_usdt} USDT`); const quote = await client.get<{ required_collateral_btc: string; btc_price_usd: string }>("/v1/quote", { principal_usdt: "1000.00", product: product.code }); console.log(`1000.00 USDT needs ${quote.body.required_collateral_btc} BTC at ${quote.body.btc_price_usd} USD (indicative)`); if (clientId.startsWith("bpc_test_")) { // One key for this request. A retry after a timeout sends the same key and gets the stored answer. const key = randomUUID(); const application = { principal_usdt: "1000.00", external_reference: `snippet-${key}`, borrower: { full_name: "Snippet Borrower", email: `snippet-${key.slice(0, 8)}@partner-sandbox.test`, country: "AR" }, }; const first = await client.post<{ id: string; status: string }>("/v1/applications", application, key); const again = await client.post<{ id: string; status: string }>("/v1/applications", application, key); console.log(`Application ${first.body.id} is ${first.body.status}; the retry replayed it: ${again.replayed && again.body.id === first.body.id}`); } let events = 0; for await (const event of client.paginate<{ id: string; type: string }>("/v1/events", { limit: 50 }, "after")) { if (++events > 5) break; console.log(`Event ${event.id} ${event.type}`); } let loans = 0; for await (const _loan of client.paginate<{ reference: string }>("/v1/loans", { limit: 100 })) loans += 1; console.log(`${loans} loan(s) in all`); } main().catch((err: unknown) => { if (err instanceof BorrouApiError) console.error(`${err.message} (request ${err.requestId ?? "unknown"})`); else console.error(err instanceof Error ? err.message : err); process.exit(1); });