DELETE/us/order/cancel
Cancel Order
API to cancel an open US stocks order. Pass the order_id you received when you placed the order. A successful request returns the same order_id. Use Get Order Details to confirm the final status. Cancel Order allows 3 requests per second. See US Stocks rate limits.
- You can cancel an order whose status is
open,scheduled,queuedorpartially_complete. This includes an after-market order waiting for the next regular session. - An order that has already finished (
complete,cancelled,cancelled_partially,failed,failed_partiallyorexpired) can't be cancelled. The request returnsUDAPI100040. - US orders can't be modified. To change an open order, cancel it and place a new order.
- If the request returns
UDAPI1319, the cancellation may still have gone through. Check the order book before you retry.
Query Parameters
| Name | Required | Type | Description |
|---|---|---|---|
| order_id | Required | string | ID of the order to cancel. Accepts alphanumeric characters and -. |
Request
curl --location --request DELETE 'https://api.upstox.com/v2/us/order/cancel?order_id=1a9ecf08-e5f8-475c-a7c0-9056826d4b18' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer {your_access_token}'
For samples in other languages, see the Sample Code section on this page.
Responses
- 200
- 4XX
Response Body
{
"status": "success",
"data": {
"order_id": "1a9ecf08-e5f8-475c-a7c0-9056826d4b18"
}
}
| Name | Type | Description |
|---|---|---|
| status | string | Outcome of the request. Typically success. |
| data | object | Response data for the cancel order request. |
| data.order_id | string | ID of the cancelled order. |
Error codes
| Error code | Description |
|---|---|
| UDAPI1023 | Order id is required - Provide order_id. |
| UDAPI1010 | Order id accepts only alphanumeric characters and '-' - The order_id format is invalid. |
| UDAPI100049 | Access to this API has been restricted for your account. Please use 'Uplink Business' to place/modify/cancel the order. - Use Uplink Business for order operations. |
| UDAPI100040 | Cancel of already cancelled/rejected/completed order is not allowed - The order has already finished, so it can no longer be cancelled. |
| UDAPI100010 | Order not found - Returned with HTTP 404 when no US order exists with this order_id. |
| UDAPI1316 | US stocks is not activated for this account - Returned with HTTP 403. |
| UDAPI1317 | US stocks order service is unavailable. Please try again later. - Returned with HTTP 423. |
| UDAPI1318 | US stocks trading is in its daily maintenance window. Please try again later. - Returned with HTTP 423. |
| UDAPI100500 | US order was rejected - The US broker rejected the request. The error message carries the broker's reason. |
| UDAPI1319 | The US order service did not confirm this request in time. Check your order book before retrying. - Returned with HTTP 500. The order may have been placed or cancelled. |
Sample Code
Cancel an open order
- Python
- Node.js
- Java
- PHP
- Python SDK
- Node.js SDK
- Java SDK
- PHP SDK
- .NET SDK
import requests
url = 'https://api.upstox.com/v2/us/order/cancel?order_id=1a9ecf08-e5f8-475c-a7c0-9056826d4b18'
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer {your_access_token}',
}
response = requests.delete(url, headers=headers)
print(response.status_code)
print(response.json())
const axios = require('axios');
const url = 'https://api.upstox.com/v2/us/order/cancel?order_id=1a9ecf08-e5f8-475c-a7c0-9056826d4b18';
const headers = {
'Accept': 'application/json',
'Authorization': 'Bearer {your_access_token}',
};
axios.delete(url, { headers })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error.response ? error.response.data : error.message);
});
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.upstox.com/v2/us/order/cancel?order_id=1a9ecf08-e5f8-475c-a7c0-9056826d4b18"))
.header("Accept", "application/json")
.header("Authorization", "Bearer {your_access_token}")
.DELETE()
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}
<?php
$url = 'https://api.upstox.com/v2/us/order/cancel?order_id=1a9ecf08-e5f8-475c-a7c0-9056826d4b18';
$headers = [
'Accept: application/json',
'Authorization: Bearer {your_access_token}',
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
} else {
echo $response;
}
curl_close($ch);
?>
import upstox_client
from upstox_client.rest import ApiException
configuration = upstox_client.Configuration()
configuration.access_token = '{your_access_token}'
api_instance = upstox_client.UsStocksOrderApi(upstox_client.ApiClient(configuration))
try:
api_response = api_instance.cancel_us_order('1a9ecf08-e5f8-475c-a7c0-9056826d4b18')
print(api_response)
except ApiException as e:
print("Exception when calling UsStocksOrderApi->cancel_us_order: %s\n" % e)
let UpstoxClient = require('upstox-js-sdk');
let defaultClient = UpstoxClient.ApiClient.instance;
var OAUTH2 = defaultClient.authentications['OAUTH2'];
OAUTH2.accessToken = "{your_access_token}";
let apiInstance = new UpstoxClient.USStocksOrderApi();
apiInstance.cancelUsOrder("1a9ecf08-e5f8-475c-a7c0-9056826d4b18", (error, data, response) => {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + JSON.stringify(data));
}
});
import com.upstox.ApiClient;
import com.upstox.ApiException;
import com.upstox.Configuration;
import com.upstox.api.UsOrderResponse;
import com.upstox.auth.OAuth;
import io.swagger.client.api.UsStocksOrderApi;
public class Main {
public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();
OAuth OAUTH2 = (OAuth) defaultClient.getAuthentication("OAUTH2");
OAUTH2.setAccessToken("{your_access_token}");
UsStocksOrderApi apiInstance = new UsStocksOrderApi();
String orderId = "1a9ecf08-e5f8-475c-a7c0-9056826d4b18";
try {
UsOrderResponse result = apiInstance.cancelUsOrder(orderId);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling UsStocksOrderApi#cancelUsOrder");
e.printStackTrace();
}
}
}
<?php
$config = (new Upstox\Client\Configuration())
->setAccessToken('{your_access_token}');
$apiInstance = new Upstox\Client\Api\USStocksOrderApi(
new GuzzleHttp\Client(),
$config
);
try {
$result = $apiInstance->cancelUsOrder('1a9ecf08-e5f8-475c-a7c0-9056826d4b18');
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling USStocksOrderApi->cancelUsOrder: ' . $e->getMessage();
}
using UpstoxClient.Api;
using UpstoxClient.Client;
using UpstoxClient.Extensions;
using UpstoxClient.Model;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
var host = Host.CreateDefaultBuilder()
.ConfigureApi((context, services, options) =>
{
options.AddTokens(new OAuthToken("{your_access_token}"));
}).Build();
await host.StartAsync();
var apiInstance = host.Services.GetRequiredService<IUSStocksOrderApi>();
try
{
var response = await apiInstance.CancelUsOrderAsync("1a9ecf08-e5f8-475c-a7c0-9056826d4b18");
Console.WriteLine(response.RawContent);
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
await host.StopAsync();