> For the complete documentation index, see [llms.txt](https://dev.init.capital/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dev.init.capital/guides/basic-interaction/borrow-and-repay.md).

# Borrow and Repay

## Borrow

Users can borrow tokens into their positions. Only the position owner or approved party to modify can call this function. The function returns a user's debt shares from the total borrows of the lending pool. Users can specify the receiver address to receive the tokens.

{% hint style="info" %}
The position must be in a mode that allows the borrow token to be used.
{% endhint %}

```solidity
// Example borrow function
function borrow(uint256 posId, address lendingPool, uint256 amount, address receiver) external returns (uint256 debtShares) {
    // 1. borrow tokens to a position
    debtShares = IInitCore(INIT_CORE).borrow(lendingPool, amount, posId, receiver);
}
```

## Repay

Users can repay their positions to increase the position's health factor.

{% hint style="info" %}
Users must have underlying tokens in their wallet and pre-approve the token to InitCore.
{% endhint %}

{% hint style="info" %}
For best practice, it is recommended that the caller approves the contract with an amount slightly higher than the pre-calculated debt amount, since the interest may accrue over time between the transaction submission to the network and the actual execution and inclusion onto the blockchain.
{% endhint %}

```solidity
// Example repay function
function repay(uint256 posId, address lendingPool, uint256 repayShares) external returns (uint256 repaidAmount) {
    // 0. .. pull in repay tokens from the caller ..
    
    // 1. calculate the token amount to repay
    uint256 repayAmount = ILendingPool(lendingPool).debtShareToAmtCurrent(repayShares);
    
    // 2. user approves underlying token with a bit extra amount
    IERC20(underlyingToken).safeApprove(INIT_CORE, repayAmount);
    
    // 3. repay
    repaidAmount = IInitCore(INIT_CORE).repay(lendingPool, repayShares, posId);
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://dev.init.capital/guides/basic-interaction/borrow-and-repay.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
