> ## Documentation Index
> Fetch the complete documentation index at: https://kamino.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Withdraw via the API

> The API generates the unsigned transaction, and the client signs it securely

Withdraw collateral from Kamino reserve using the API. The API builds the transaction, and you sign it locally keeping your keys secure.

## Withdraw via the API

<Steps>
  <Step>
    ### Import Dependencies

    Import the required packages for Solana RPC communication, transaction handling, and Kamino SDK utilities.

    ```typescript theme={null}
    import {
      createSolanaRpc,
      createSolanaRpcSubscriptions,
      getTransactionDecoder,
      getCompiledTransactionMessageDecoder,
      decompileTransactionMessageFetchingLookupTables,
      pipe,
      setTransactionMessageFeePayerSigner,
      setTransactionMessageLifetimeUsingBlockhash,
      addSignersToTransactionMessage,
      signTransactionMessageWithSigners,
      sendAndConfirmTransactionFactory,
      getSignatureFromTransaction,
    } from '@solana/kit';
    import { parseKeypairFile } from '@kamino-finance/klend-sdk/dist/utils/signer';
    ```
  </Step>

  <Step>
    ### Configure and Initialize

    Set up the configuration and initialize RPC connections.

    ```typescript theme={null}
    const KEYPAIR_FILE = '/path/to/your/keypair.json';
    const API_BASE_URL = 'https://api.kamino.finance';
    const RPC_ENDPOINT = 'https://api.mainnet-beta.solana.com';
    const WS_ENDPOINT = 'wss://api.mainnet-beta.solana.com';

    const MAIN_MARKET = '7u3HeHxYDLhnCoErrtycNokbQYbWGzLs6JSDqGAv5PfF';
    const CASH_RESERVE = 'ApQkX32ULJUzszZDe986aobLDLMNDoGQK8tRm6oD6SsA';

    const signer = await parseKeypairFile(KEYPAIR_FILE);
    const rpc = createSolanaRpc(RPC_ENDPOINT);
    const rpcSubscriptions = createSolanaRpcSubscriptions(WS_ENDPOINT);
    ```
  </Step>

  <Step>
    ### Call the Kamino API

    Make a POST request to the Kamino API to build the withdraw transaction.

    ```typescript theme={null}
    const res = await fetch(`${API_BASE_URL}/ktx/klend/withdraw`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        wallet: signer.address,
        market: MAIN_MARKET,
        reserve: CASH_RESERVE,
        amount: '2.0',
      }),
    });

    const data = await res.json();

    if (!res.ok) {
      console.error('Response error:', data);
    }

    const encodedTransaction = data.transaction;
    ```

    <Info>
      The API returns a pre-built transaction encoded in base64 format ready for signing.
    </Info>
  </Step>

  <Step>
    ### Decode Transaction

    Decode the base64-encoded transaction and extract the message bytes.

    ```typescript theme={null}
    const txBuffer = Buffer.from(encodedTransaction, 'base64');
    const txMessageBytes = getTransactionDecoder().decode(txBuffer).messageBytes;
    const compiledMessage = getCompiledTransactionMessageDecoder().decode(txMessageBytes);
    ```
  </Step>

  <Step>
    ### Resolve Address Lookup Tables and Sign

    Fetch the latest blockhash, decompile the transaction to resolve Address Lookup Tables, and sign.

    ```typescript theme={null}
    const { value: latestBlockhash } = await rpc.getLatestBlockhash({
      commitment: 'finalized'
    }).send();

    const signedTransaction = await pipe(
      await decompileTransactionMessageFetchingLookupTables(compiledMessage, rpc),
      tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
      tx => setTransactionMessageFeePayerSigner(signer, tx),
      tx => addSignersToTransactionMessage([signer], tx),
      tx => signTransactionMessageWithSigners(tx)
    );
    const signature = getSignatureFromTransaction(signedTransaction);
    ```

    <Info>
      `decompileTransactionMessageFetchingLookupTables` fetches the lookup table data from the blockchain and resolves all address references. This is required for transactions that use ALTs.
    </Info>
  </Step>

  <Step>
    ### Send and Confirm Transaction

    Submit the signed transaction to the Solana network.

    ```typescript theme={null}
    const sendAndConfirm = sendAndConfirmTransactionFactory({
      rpc,
      rpcSubscriptions,
    });

    await sendAndConfirm(signedTransaction, {
      commitment: 'confirmed',
      skipPreflight: false,
    });

    console.log('Withdraw successful! Signature:', signature);
    ```

    <Check>
      Your withdrawal is complete! The API built the transaction, you signed it locally (keeping your keys secure), and it was successfully submitted to the network.
    </Check>
  </Step>
</Steps>
