> For the complete documentation index, see [llms.txt](https://xpansionchain-1.gitbook.io/xpansionchain/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xpansionchain-1.gitbook.io/xpansionchain/advanced-guides/link-sdk/link.buy.md).

# Link.buy

## Link.buy

LINK REFERENCE TOOL

Check out our <mark style="color:blue;">**Link reference tool**</mark> to understand how `Link` methods work without having to write any code.

Here's how you can initiate a buy order:

```
import { Link, XpansionChainClient, XpansionChainOrderStatus} from ‘@imtbl/imx-sdk’;
// Pass orderAndTradeAPI version parameter as 'v3' to target the latest version of order & trade API.
const link = new Link('https://link.sandbox.x.XpansionChain.com', null, 'v3')
buyResults: BuyResponse = await link.buy({ orderIds: ['1', '2', '3'] });
```

Just like all other link SDK methods, **buy** returns a promise, which resolves once all operations are complete, or rejects once the link encounters a critical error.

Once a multi-buy flow is confirmed by the user, **Buy** will asynchronously try to buy each order in the list and then display the results.

In order to handle the various new kinds of error states that this functionality allows, `link.buy` will now resolve with a response report payload which matches the following types:

```
type SuccessOrError = 'success' | 'error';

type OrderStatus = {
  status: SuccessOrError;
  message?: string;
};

type BuyResults = {
  [orderId: string]: OrderStatus;
};

type BuyResponse = {
  result: BuyResults;
};
```

An example resolve payload from a semi-successful `buy()` flow:

```
{
  result: {
     '1': { status: 'success' },
     '2': {
        status: 'error',
        message: 'Cannot purchase own order',
     },
     '3': { status: 'success' },
  }
}
```

If the multi-buy flow is found to contain 0 valid orders after minimal client-side validation, then link will error out (and reject the promise it returned from the buy() method) and no orders will be purchased.

### Errors[​](https://docs.x.immutable.com/docs/x/link-buy2#errors) <a href="#errors" id="errors"></a>

See error responses <mark style="color:blue;">here</mark>.
