# Getting started

This guide takes you from credentials to a reconciled order over REST. A
latency-sensitive strategy then sends the same orders on the
[write WebSocket](/api/exchange-websocket).

## 1. Set your credentials

Mosaiq access is tied to an approved EVM wallet. That wallet address is your
public Mosaiq `user`.

If you received an invitation, [redeem it](/api/http-public#redeem-an-invitation)
with your Para session. Redeeming approves the wallet. Do it before you create
a key.

Create an API key from an authenticated Para wallet session:

```bash
curl https://production.mosaiq.0d.finance/v1/apiKeys \
  -H "Authorization: Bearer $PARA_JWT" \
  -H "Content-Type: application/json" \
  -d '{}'
```

* If the Para session holds several EVM wallets, also send
  `X-Tessera-Wallet: <approved-wallet>`.
* The response shows the raw key once. Store it.
* The body `{}` creates a key for the master account, whose address is your
  wallet.
* Creating a key revokes the previous key of the same account.
* A key expires after 30 days, unless the body sets `expiresAtMs`, a Unix
  millisecond timestamp at most 365 days away.

[API keys](/api/api-key) has the full contract.
[Sub-accounts](/concepts/sub-accounts) explains how to trade from more than one
account.

```bash
export MOSAIQ_API_BASE_URL=https://production.mosaiq.0d.finance
export MOSAIQ_USER=0x0000000000000000000000000000000000000001
export MOSAIQ_API_KEY='<your-api-key>'
```

Trading requests authenticate with `Authorization: Bearer <your-api-key>`.

## 2. Inspect the account and market catalog

```bash
curl "$MOSAIQ_API_BASE_URL/v1/clearinghouseState?user=$MOSAIQ_USER"
```

Read these fields of the response:

| Field | What it tells you |
| --- | --- |
| `data.userData.marginSummary.availableFunds` | The margin left to open new risk. |
| `data.userData.userCaps` | The limits of the owner, which all its accounts share. |
| `data.venueData.meta.venues` | Whether Mosaiq can reach each venue. |
| `data.assetData.universe[*].venues[*]` | The venue code, the `lifecycle` (which orders Mosaiq takes), the tick size, the size increment, and the minimum notional of each market. |

Use `assetData.universe[*].name` as the order field `a`, and
`assetData.universe[*].venues[*].venue` exactly as returned. Do not derive venue
symbols or precision rules in the client.

## 3. Start lifecycle subscriptions

Open the user-data WebSocket before the first write and subscribe to
`orderUpdates` and `userFills`:

```ts
const apiBaseUrl = 'https://production.mosaiq.0d.finance'
const user = '0x0000000000000000000000000000000000000001'

const url = new URL('/v1/ws', apiBaseUrl)
url.protocol = 'wss:'
url.searchParams.set('user', user)

const ws = new WebSocket(url)

ws.addEventListener('open', () => {
  for (const type of ['orderUpdates', 'userFills']) {
    ws.send(JSON.stringify({ method: 'subscribe', subscription: { type } }))
  }
})

ws.addEventListener('message', (event) => {
  const message = JSON.parse(event.data)
  if (message.status !== 'ok') return

  if (message.topic === 'orderUpdates') {
    for (const order of message.data.orderUpdates) {
      console.log('order', order.cloid, order.status)
    }
  }

  if (message.topic === 'userFills') {
    for (const fill of message.data.userFills) {
      console.log('fill', fill.cloid, fill.fillId)
    }
  }
})
```

The first message of each topic is a snapshot. Store order transitions by
`cloid` and fills by `(venue, fillId)`. Make that processing idempotent: the
same row can arrive twice.

## 4. Fund the account

```bash
curl "$MOSAIQ_API_BASE_URL/v1/exchange" \
  -H "Authorization: Bearer $MOSAIQ_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"type":"deposit","amount":"100"}'
```

Send exactly `amount` USDC on Arbitrum, from the registered wallet, to the
returned `to` address before `expiresAtMs`. The minimum is 1 USDC. Mosaiq
credits the master account when it sees a transfer whose sender and amount
match the request. [Deposit](/api/exchange#deposit) lists the transfers that
wait for a manual review.

Check the balance with
[`GET /v1/clearinghouseState`](/api/clearinghouse-state), and the request
status with [`GET /v1/funding`](/api/funding) (Para JWT). To fund a
sub-account, send a [`transfer`](/api/exchange#transfer) from the master
account.

## 5. Submit an order

Choose an active market and values that satisfy the selected venue's live
constraints. Give every placement a fresh, non-zero, lowercase 128-bit
hexadecimal `cloid`.

```bash
export MOSAIQ_ASSET=BTC
export MOSAIQ_VENUE=hyperliquid
export MOSAIQ_LIMIT_PRICE='<current-post-only-price>'
export MOSAIQ_SIZE='<valid-base-size>'
export MOSAIQ_CLOID=0x00000000000000000000000000000001

curl "$MOSAIQ_API_BASE_URL/v1/exchange" \
  -H "Authorization: Bearer $MOSAIQ_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "type": "place",
  "orders": [
    {
      "a": "$MOSAIQ_ASSET",
      "b": "buy",
      "p": "$MOSAIQ_LIMIT_PRICE",
      "s": "$MOSAIQ_SIZE",
      "venue": "$MOSAIQ_VENUE",
      "t": "Alo",
      "c": "$MOSAIQ_CLOID"
    }
  ]
}
JSON
```

```jsonc
{
  "status": "ok",
  "data": [
    {
      "status": "accepted",
      "orderId": "...",
      "cloid": "0x00000000000000000000000000000001",
      "venue": "hyperliquid",
      "size": "<accepted-size>"
    }
  ],
  "server_time_ms": 1780000000100,
  "server_receive_time_ms": 1780000000088
}
```

`accepted` means that Mosaiq launched the venue send. It does not mean that the
venue took the order or that it filled. Track the order by `cloid` on
`orderUpdates`, read executions from `userFills`, and read balances and
positions from `clearinghouseState`.

## 6. Cancel or reconcile

```bash
curl "$MOSAIQ_API_BASE_URL/v1/exchange" \
  -H "Authorization: Bearer $MOSAIQ_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "type": "cancel",
  "orders": [
    { "venue": "$MOSAIQ_VENUE", "c": "$MOSAIQ_CLOID" }
  ]
}
JSON
```

If a request or a connection ends without a final answer, do not resend the
request at once. First:

1. Reconnect and read the fresh `orderUpdates` and `userFills` snapshots.
2. Query [`GET /v1/openOrders?user=<user>&cloid=<cloid>`](/api/open-orders).
3. Backfill fills with [`GET /v1/userFills`](/api/user-fills).

Then decide whether to retry.

Next: [Market data WebSocket](/api/market-data-websocket),
[Write WebSocket](/api/exchange-websocket),
[Optimizing latency](/api/optimizing-latency), and the
[Exchange API](/api/exchange) reference.
