> For the complete documentation index, see [llms.txt](https://docs.polynomial.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.polynomial.fi/developer-documentation/api/create-order.md).

# Create Order

### Create Market Order (nitro mode)

`POST`  `https://orderbook-mainnet.polynomial.finance/api/market_order/<market_id>`

#### Query&#x20;

| Name       | Description                                                                                                                    |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------ |
| market\_id | You can find market id from markets api response. [Markets](/developer-documentation/api/markets.md#retrieve-all-markets-info) |

#### Headers&#x20;

| Name         | Description                                                                            |
| ------------ | -------------------------------------------------------------------------------------- |
| Content-Type | application/json                                                                       |
| Accept       | application/json                                                                       |
| x-api-key    | [Introduction](/developer-documentation/api/introduction.md#backend-api-key)(optional) |

#### Request body

> **Note:** All fields are **required**, even if not currently used. Fields like `allowAggregation`, `allowPartialMatching`, and `expiration` are required for schema compliance but are not used functionally.

| Name                 | Type    | Description                                                                                                                                                                                                                                             |
| -------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| acceptablePrice      | string  | Execution price with expected slippage [example](https://github.com/Polynomial-Protocol/polynomial-starter-kit/blob/5dbaf415ac79d41352d6330e0cfcba9e7977cfa2/lib/order.ts#L17) . More details [here](/developer-documentation/api/price.md#fill-price). |
| accountId            | string  | Perp Account Id. Returned by [accounts api ](/developer-documentation/api/account.md#retrieve-account-info)                                                                                                                                             |
| allowAggregation     | boolean | true **(Required but unused)**                                                                                                                                                                                                                          |
| allowPartialMatching | boolean | true **(Required but unused)**                                                                                                                                                                                                                          |
| chainId              | number  | 8008 (mainnet)                                                                                                                                                                                                                                          |
| expiration           | string  | Order expirate time, in ms. **(Required but unused)**                                                                                                                                                                                                   |
| id                   | string  | <p>Market order signature signed by API wallet key. More details <br><a data-mention href="#signature-generation">#signature-generation</a></p>                                                                                                         |
| marketId             | string  | From `markets` api response. [Markets](/developer-documentation/api/markets.md#retrieve-all-markets-info)                                                                                                                                               |
| nonce                | string  | Use a function generating increasing value. [Example](https://github.com/Polynomial-Protocol/polynomial-starter-kit/blob/5dbaf415ac79d41352d6330e0cfcba9e7977cfa2/lib/nitromode.ts#L154)                                                                |
| referrerOrRelayer    | string  | `0x4D387f5c0Ec87e47b9Df9b8C97B89D2977431b27`                                                                                                                                                                                                            |
| settlementStrategyId | string  | `0`                                                                                                                                                                                                                                                     |
| sizeDelta            | string  | With 18 decimals precision. Ex `-1000000000000000000` for shorting -1 size                                                                                                                                                                              |
| trackingCode         | string  | `0x0000000000000000000000000000000000000000000000000000000000000000`                                                                                                                                                                                    |
| reduceOnly           | boolean | If `true`, ensures the order only reduces an existing position.                                                                                                                                                                                         |
|                      |         |                                                                                                                                                                                                                                                         |

#### Signature Generation&#x20;

The **Nitro Mode Market Order API** requires **signed data** for transaction execution.

```typescript
export interface OffchainOrder {
  marketId: string;
  accountId: string;
  sizeDelta: string;
  chainId: number;
  settlementStrategyId: string;
  referrerOrRelayer: `0x${string}`;
  allowAggregation: boolean;
  allowPartialMatching: boolean;
  reduceOnly: boolean;
  acceptablePrice: string;
  trackingCode: `0x${string}`;
  expiration: string;
  nonce: string;
}

export async function signOffchainOrder(
  privateKey: `0x${string}`,
  order: OffchainOrder,
  isMainnet: boolean = false,
) {

  const rpcUrl = isMainnet ? "https://rpc.sepolia.polynomial.fi/" : "https://rpc.sepolia.polynomial.fi/";
  const types = {
    OffchainOrder: [
      { name: 'marketId', type: 'uint128' },
      { name: 'accountId', type: 'uint128' },
      { name: 'sizeDelta', type: 'int128' },
      { name: 'settlementStrategyId', type: 'uint128' },
      { name: 'referrerOrRelayer', type: 'address' },
      { name: 'allowAggregation', type: 'bool' },
      { name: 'allowPartialMatching', type: 'bool' },
      { name: 'reduceOnly', type: 'bool' },
      { name: 'acceptablePrice', type: 'uint256' },
      { name: 'trackingCode', type: 'bytes32' },
      { name: 'expiration', type: 'uint256' },
      { name: 'nonce', type: 'uint256' },
    ],
  };

  const walletClient = createWalletClient({
    transport: http(rpcUrl),
    account: privateKeyToAccount(privateKey),
  });

  const mainnetDomain = {
    name: 'PolynomialPerpetualFutures',
    version: '1',
    chainId: 8008,
    verifyingContract: "0xD052Fa8b2af8Ed81C764D5d81cCf2725B2148688" as `0x${string}`,
  };
  const testnetDomain = {
    name: 'PolynomialPerpetualFutures',
    version: '1',
    chainId: 80008,
    verifyingContract: "0x52Fdc981472485232587E334c5Ca27F241CbA9AA" as `0x${string}`,
  };
  

  return await walletClient.signTypedData({
    account: privateKeyToAccount(privateKey),
    domain: isMainnet ? mainnetDomain : testnetDomain,
    types,
    primaryType: 'OffchainOrder',
    message: order as any,
  });
}
```

You can find a working TypeScript example [here](https://github.com/Polynomial-Protocol/polynomial-starter-kit/blob/5dbaf415ac79d41352d6330e0cfcba9e7977cfa2/lib/nitromode.ts#L17).

### On-Chain Order via Contract Call

API wallets are permitted to create market orders directly through a **smart contract call**.\
Note: Since this method does **not use a relayer**, there may be a slight delay in order settlement.

For more details, refer to the [documentation](/developer-documentation/perps-core/connector-contracts.md)
