# Borrow with the SDK

Take a loan in code: sign in, quote, sign the terms, fund, repay. The SDK rebuilds and checks every term before the wallet signs.

Loans have no sandbox, so this runs on mainnet with your live key, real BTC and real gas. Start small: the smallest loan is 7 USDC. Keep both private keys in your own key store, never in code.

```ts
import { bitcoinSignerFromKey, createMaxyClient, evmSignerFromPrivateKey } from '@maxy-labs/sdk';
import { parseUnits } from 'viem';

let accessToken: string | null = null;
const maxy = createMaxyClient({ getAccessToken: () => accessToken }); // reads MAXY_API_KEY
const evm = evmSignerFromPrivateKey(process.env.EVM_PRIVATE_KEY!); // in a browser, the user's wallet
const bitcoin = bitcoinSignerFromKey(process.env.BTC_PRIVATE_KEY!);
const market = 'base-mainnet'; // or 'robinhood-mainnet'

// Sign in: every request acts for this wallet. A wallet's first sign-in needs a username.
const { session } = await maxy.auth.signIn(evm, { username: 'user-1234' });
accessToken = session.accessToken;

// Pick a vault and quote 10 USDC.
const [vault] = await maxy.borrow.listVaults({ market });
const quote = await maxy.borrow.quote({ market, vaultId: vault.id, principal: parseUnits('10', 6) });
if (!quote.canBorrow) throw new Error(quote.blockedReason ?? 'quote blocked'); // on click: show the quote first

// Ask for terms, sign them. Maxy's service commits the loan.
const request = await maxy.borrow.createRequest({ quote, borrower: evm, bitcoin });
await maxy.borrow.signTerms(request, evm);
const committed = await maxy.borrow.waitForCommit(request.requestId, { market });

// Fund: sign the returns, then send the BTC.
const funded = await maxy.borrow.fund({ market, requestId: request.requestId, bitcoin, signedTermsHash: request.termsHash });

// Follow the loan until the principal is credited, then withdraw it.
for await (const view of maxy.borrow.trackLoan({ market, loanId: committed.loanId, intervalMs: 60_000 })) {
  if (view.status.key === 'active') break;
}
await maxy.borrow.claimPayout({ market, evm, loanId: committed.loanId });

// Later, repay. Maxy's service reveals the secret and returns the BTC.
await maxy.borrow.repay({ market, loanId: committed.loanId, evm });
```

## If it is refused

| Case | Do |
| --- | --- |
| `quote.canBorrow` is false | Show the quote. Keep the button live, and show `blockedReason` on click |
| `insufficient_liquidity` from `createRequest` | The vault cannot lend this amount now. Try less |
| `verification_failed` | The plan failed a local check. Nothing is signed |
| `funded.outcome.status` is `ambiguous` | Do not fund again. Look the txid up first |
| `insufficient_funds`, `serverCode` `no_gas` | `maxy.borrow.requestGas({ loanId })`, then retry |

Every call: [SDK reference in the guide PDF](https://maxy.co/guides/maxy-integration-guide.pdf).

---
Source: https://maxy.co/docs/borrow-with-the-sdk (Maxy Docs › Loans (MaxyLock) › Build with it)
