> For the complete documentation index, see [llms.txt](https://docs.aftermath.finance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aftermath.finance/for-developers/api/rest-api/dca-and-limit-orders.md).

# DCA & Limit Orders

REST endpoints for DCA and spot limit orders.

> DCA and spot limit-order reads, signed actions, and transaction builders.

These APIs return frontend-ready serialized transactions. They do not use the CCXT build-sign-submit response shape.

Authenticated reads/actions use the reusable terms signature described in `authentication.md`: the base64-decoded `bytes` must be exactly `Aftermath Terms and Conditions`. Order IDs and other route data are plain request fields, not signed JSON.

## DCA Routes

```
POST /api/dca/active
POST /api/dca/past
POST /api/dca/cancel
POST /api/dca/transactions/create-order
```

Deprecated compatibility routes remain available:

```
POST /api/dca/user/add  // { walletAddress, bytes, signature } -> boolean
POST /api/dca/user/get  // { walletAddress } -> base64 public-key bytes or null
```

The `user/add` signature uses the legacy user-data protocol, not the reusable terms message. Prefer `/api/user-data/public-key` and `/api/user-data/save-public-key` for new public-key storage integrations; these DCA paths are legacy aliases.

### Read orders

`active` and `past` require the same body:

```json
{ "walletAddress": "0x..." }
```

Both return a bare `DcaOrderObject[]`, sorted by creation time descending.

Important response fields:

* `objectId`: DCA order object ID.
* `overview.allocatedCoin`, `overview.buyCoin`, and `overview.totalSpent`: amounts are `"...n"` strings.
* `overview.intervalMs`, `overview.totalTrades`, `overview.tradesRemaining`, and `overview.maxSlippageBps`: JSON numbers.
* `overview.strategy`: nullable `{ minPrice, maxPrice }`, with `"...n"` price strings.
* `overview.created`, `overview.nextTrade`, and optional `overview.lastExecutedTrade`: transaction timing data.
* `trades`: completed executions; coin amounts are decimal strings without an added `n`.
* `failed`: `{ timestamp, reason }[]`.

`OrderTxData` includes the canonical `timestamp` and `txnDigest` fields plus the legacy duplicates `time` and misspelled `tnx_digest`.

### Create a DCA order transaction

```typescript
type CreateDcaOrderRequest = {
  allocateCoinAmount: string;
  walletAddress: string;
  allocateCoinType: string;
  buyCoinType: string;
  frequencyMs: number;
  tradesAmount: number;
  delayTimeMs: number;
  maxAllowableSlippageBps: number;
  coinPerTradeAmount: string;
  isSponsoredTx: boolean;
  strategy?: { minPrice: string; maxPrice: string } | null;
  customRecipient?: string | null;
  integratorFee?: { feeRecipient: string; feeBps: number } | null;
};
```

Request rules:

* `allocateCoinAmount`, `coinPerTradeAmount`, and strategy prices may include a trailing `n`.
* `tradesAmount` must not exceed `255`. Oversized requests use error code `2007`.
* Omitting `strategy` uses the full price range.
* An absent or empty `customRecipient` defaults to `walletAddress`.
* An absent `integratorFee` uses a zero fee and zero address.
* Dry-run failures use error code `2008`.

The response is a bare JSON string containing the serialized transaction.

### Cancel a DCA order

`POST /api/dca/cancel` requires signed authentication:

```json
{
  "walletAddress": "0x...",
  "bytes": "<base64 of Aftermath Terms and Conditions>",
  "signature": "<signature over the decoded terms bytes>",
  "orderObjectIds": ["0x...", "0x..."]
}
```

The response is an empty JSON object on success. The service verifies the wallet/terms signature, then uses the plain `orderObjectIds` to select the orders. Do not put the IDs inside the signed bytes.

## Spot Limit-Order Routes

```
POST /api/limit-orders/active
POST /api/limit-orders/past
POST /api/limit-orders/cancel
POST /api/limit-orders/min-order-size-usd
POST /api/limit-orders/transactions/create-order
```

### Reads and cancellation

* `active` requires `walletAddress`, `bytes`, and `signature`.
* `past` requires only `walletAddress`.
* `active` and `past` return a bare `LimitOrderObject[]`, sorted by creation time descending.
* Response objects use camelCase fields such as `objectId`, `allocatedCoin`, `buyCoin`, `currentAmountSold`, `currentAmountBought`, `expiryTimestamp`, `integratorFee`, and `outputToInputStopLossExchangeRate`.
* `allocatedCoin.amount`, `buyCoin.amount`, `currentAmountSold`, and `currentAmountBought` are `"...n"` strings.
* `created` and optional `finished` are `{ timestamp, txnDigest }`.
* `cancel` requires the same signed fields as `active`, plus plain `orderObjectIds: string[]`, and returns a bare boolean.
* `min-order-size-usd` accepts a bodyless POST and returns a bare USD number.

### Create a limit-order transaction

```typescript
type CreateLimitOrderRequest = {
  walletAddress: string;
  allocateCoinType: string;
  allocateCoinAmount: string;
  buyCoinType: string;
  expiryDurationMs: number;
  outputToInputExchangeRate: number;
  isSponsoredTx: boolean;
  customRecipient?: string | null;
  integratorFee?: { feeRecipient: string; feeBps: number } | null;
  outputToInputStopLossExchangeRate?: number | null;
};
```

Request rules:

* `allocateCoinAmount` may include a trailing `n`.
* An absent or empty `customRecipient` defaults to `walletAddress`.
* An absent integrator fee uses a zero fee and zero address.
* An absent stop-loss exchange rate disables the stop loss.
* Dry-run failures use error code `2009`.

The response is a bare JSON string containing the serialized transaction.

The v3.3.1 SDK types carry the current plain `orderObjectIds` fields for cancellation. Use `UserData.createTermsAndConditionsMessage()` for the reusable terms bytes; the deprecated `*MessageToSign` action builders are not the service signature payload and should not be used for current raw requests.

## Errors

Do not assume all failures use the OpenAPI `ErrorResponse` object. The API can return a JSON string with error headers, plain text, or a structured body. See `error-handling.md`.
