> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rails.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Orders

> Place, modify, and cancel perpetuals orders with the Go SDK, recover from a command that got no answer, and pace your requests.

This page builds a program that places a post-only bid, replaces it with a larger one, and cancels it. Any command that gets no answer is reconciled instead of sent again, and every command is [paced](/latest/go-sdk/configuration#pacing) to the budget of your account's type. It picks up from the [Quickstart](/latest/perps/go-sdk/quickstart).

## Place an Order

```go theme={null}
created, err := conn.CreateOrder(ctx, perps.CreateOrderRequest{
	OrderType: perps.OrderTypeBuy,
	TradeType: perps.TradeTypeLimit,
	Price:     "50000",
	Quantity:  "0.0001",
	PostOnly:  true,
})
if err != nil {
	return err
}
```

The request's fields are those of [Order Creation](/latest/perps/guides/order-creation), and every answer about the order carries its `ClientRequestID`. Commands from any number of goroutines can share one connection.

## Modify an Order

A modify [replaces the order](/latest/perps/guides/order-modification) under a new id, and the result describes the replacement:

```go theme={null}
modified, err := conn.ModifyOrder(ctx, perps.ModifyOrderRequest{
	OrderID:  created.Order.OrderID,
	Price:    "50000",
	Quantity: "0.0002",
})
if err != nil {
	return err
}
```

## Cancel an Order

```go theme={null}
cancel := perps.CancelOrderRequest{OrderID: modified.Order.OrderID}
if _, err := conn.CancelOrder(ctx, cancel); err != nil {
	return err
}
```

[Cancelling](/latest/perps/guides/order-cancellation) is idempotent by order id, and `CancelOrder` resends in the one case where the exchange itself asks for the cancel again.

## Cancel Every Order

To cancel every order on a market, or on all of them with `perps.MarketAll`, call `client.CancelAllOrders`. It goes over HTTP, so it works even when the WebSocket can't be dialled during a [soft ban](/latest/perps/guides/rate-limits#soft-ban).

## When a Command Gets No Answer

After `rails.ErrTimeout` or `rails.ErrConnClosed` the command may still have been applied, and its error carries the `clientRequestId`. Either look the order up with it, as the example does, or send the command again with that `ClientRequestID` set: within 60 seconds the exchange [refuses a repeat](/latest/perps/websocket-api/overview#request-envelope) with `DUPLICATE_REQUEST` instead of executing it twice. Only the lookup says what happened to the order.

## Clean Up After a Failure

Closing a connection doesn't cancel its orders. The program keeps the id of the order it has on the book, set to the replacement's id after the modify and cleared after the cancel, and cancels it if it stops before cancelling it itself. The cleanup gets a context of its own, since the program's may be what ran out:

```go theme={null}
live := created.Order.OrderID
defer func() {
	if live == "" {
		return
	}
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	req := perps.CancelOrderRequest{OrderID: live}
	if _, err := conn.CancelOrder(ctx, req); err != nil {
		fmt.Fprintf(os.Stderr, "cancelling %s: %v\n", live, err)
	}
}()
```

## Run the Example

The complete program is [`examples/perps-orders`](https://github.com/rails-xyz/exchange-go-sdk/tree/main/examples/perps-orders) in the SDK repository. Run it with a price well below the market:

```bash theme={null}
RAILS_API_KEY=<key> RAILS_API_SECRET=<secret> go run ./examples/perps-orders -price 50000
```

```text theme={null}
created 01M3Z1DEGXFK9CEW2MHVD1Y7A9: active
modified 01M3Z1DEGXFK9CEW2MHVD1Y7A9 into 01M3Z1DEJPD7EQB626X0Z16KX7: quantity 0.0002
cancelled 01M3Z1DEJPD7EQB626X0Z16KX7: cancelled
```

## Related

* [Order Creation](/latest/perps/guides/order-creation), [Order Modification](/latest/perps/guides/order-modification) and [Order Cancellation](/latest/perps/guides/order-cancellation)
* [Order Creation Stream](/latest/perps/websocket-api/order-creation-stream)
* [Rate Limits](/latest/perps/guides/rate-limits) and [Error Codes](/latest/perps/guides/error-codes)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.