> For the complete documentation index, see [llms.txt](https://docs.kyberswap.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kyberswap.com/developer-guide/limit-order-api/how-to-guides/fill-a-limit-order.md).

# Fill a Limit Order

Discover open limit orders and execute a fill on-chain as a Taker using the   KyberSwap Limit Order API.

## Fill a Limit Order

Takers fill limit orders on-chain, earning the spread between the order rate and the current market rate. Every fill requires a KyberSwap Operator co-signature that authorises the specific fill — this prevents stale or frontrun fills.

{% hint style="success" %}
**Full demo:** [github.com/KyberNetwork/ks-limit-order-API-demo](https://github.com/KyberNetwork/ks-limit-order-API-demo)
{% endhint %}

### Flow

```
GET /orders  →  GET /operator-signature  →  check allowance
    →  POST /encode/fill-order-to  →  send transaction
```

### Step 1 — Get Open Orders

Query open orders for a token pair, sorted by best rate (descending):

```typescript
const { data } = await axios.get(
    "https://limit-order.kyberswap.com/read-partner/api/v1/orders",
    {
        params: {
            chainId: 137,
            makerAsset: "0x2791bca1...",   // token the Maker is selling
            takerAsset: "0x1C954E8f...",   // token the Taker must provide
        }
    }
);

const orders = data.data;   // sorted best-rate first
const targetOrderId = orders[0].id;
```

{% hint style="info" %}
Orders are sorted by the effective rate formula which accounts for partial fills and fees.
{% endhint %}

### Step 2 — Get the Operator Signature

The Operator signature is required for every fill. It is short-lived and must be fetched immediately before building the transaction:

```typescript
const { data: sigData } = await axios.get(
    "https://limit-order.kyberswap.com/read-partner/api/v1/orders/operator-signature",
    { params: { chainId: 137, orderIds: targetOrderId } }
);

const operatorSignature = sigData.data[0].operatorSignature;
```

### Step 3 — Check and Set Allowance

The Limit Order contract must be approved to spend the Taker's `takerAsset`:

```typescript
const takingAmount = BigInt(orders[0].takingAmount) / 2n;  // fill half the order

const allowance = await tokenContract.allowance(signerAddress, limitOrderContract);
if (allowance < takingAmount) {
    const tx = await tokenContract.approve(limitOrderContract, takingAmount);
    await tx.wait();
}
```

### Step 4 — Encode the Fill Calldata

```typescript
const requestBody = {
    orderId: Number(targetOrderId),
    takingAmount: takingAmount.toString(),
    thresholdAmount: "0",          // minimum makingAmount to receive (slippage guard)
    target: signerAddress,         // address to receive the makerAsset
    operatorSignature: operatorSignature
};

const { data: encoded } = await axios.post(
    "https://limit-order.kyberswap.com/read-ks/api/v1/encode/fill-order-to",
    requestBody
);
```

{% hint style="info" %}
**Batch fills:** Use `POST /read-ks/api/v1/encode/fill-batch-orders-to` to fill multiple orders in one transaction. Pass arrays of `orderIds` and `operatorSignatures` in matching order. See Fill Batch Orders.
{% endhint %}

### Step 5 — Execute On-chain

```typescript
const tx = await signer.sendTransaction({
    to:    limitOrderContract,
    from:  signerAddress,
    data:  encoded.data.encodedData,
    maxFeePerGas: 100000000000,
    maxPriorityFeePerGas: 100000000000
});

const receipt = await tx.wait();
console.log("Fill tx hash:", receipt.hash);
```

### Related

* [Fee Structure](/developer-guide/limit-order-api/fee-structure.md) — protocol fees and rate calculation
* [GET /read-partner/api/v1/orders](/developer-guide/limit-order-api/api-reference/taker-api.md)
* [GET /read-partner/api/v1/orders/operator-signature](/developer-guide/limit-order-api/api-reference/taker-api.md#get-operator-signature)
* [POST /read-ks/api/v1/encode/fill-order-to](/developer-guide/limit-order-api/api-reference/taker-api.md#post-read-ks-api-v1-encode-fill-order-to)


---

# 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://docs.kyberswap.com/developer-guide/limit-order-api/how-to-guides/fill-a-limit-order.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.
