curl --request POST \
--url https://api.moonshot.ai/v1/batches/{batch_id}/cancel \
--header 'Authorization: Bearer <token>'import requests
url = "https://api.moonshot.ai/v1/batches/{batch_id}/cancel"
headers = {"Authorization": "Bearer <token>"}
response = requests.post(url, headers=headers)
print(response.text)const options = {method: 'POST', headers: {Authorization: 'Bearer <token>'}};
fetch('https://api.moonshot.ai/v1/batches/{batch_id}/cancel', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.moonshot.ai/v1/batches/{batch_id}/cancel",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.moonshot.ai/v1/batches/{batch_id}/cancel"
req, _ := http.NewRequest("POST", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.moonshot.ai/v1/batches/{batch_id}/cancel")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.moonshot.ai/v1/batches/{batch_id}/cancel")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"id": "<string>",
"object": "batch",
"endpoint": "<string>",
"input_file_id": "<string>",
"completion_window": "<string>",
"created_at": 123,
"request_counts": {
"completed": 123,
"failed": 123,
"total": 123
},
"output_file_id": "<string>",
"error_file_id": "<string>",
"in_progress_at": 123,
"expires_at": 123,
"finalizing_at": 123,
"completed_at": 123,
"failed_at": 123,
"cancelling_at": 123,
"cancelled_at": 123,
"metadata": {}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}Cancel Batch
Cancel an in-progress batch task. The status will change to cancelling and then to cancelled. Only tasks in validating, in_progress, or finalizing status can be cancelled.
curl --request POST \
--url https://api.moonshot.ai/v1/batches/{batch_id}/cancel \
--header 'Authorization: Bearer <token>'import requests
url = "https://api.moonshot.ai/v1/batches/{batch_id}/cancel"
headers = {"Authorization": "Bearer <token>"}
response = requests.post(url, headers=headers)
print(response.text)const options = {method: 'POST', headers: {Authorization: 'Bearer <token>'}};
fetch('https://api.moonshot.ai/v1/batches/{batch_id}/cancel', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.moonshot.ai/v1/batches/{batch_id}/cancel",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"net/http"
"io"
)
func main() {
url := "https://api.moonshot.ai/v1/batches/{batch_id}/cancel"
req, _ := http.NewRequest("POST", url, nil)
req.Header.Add("Authorization", "Bearer <token>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.moonshot.ai/v1/batches/{batch_id}/cancel")
.header("Authorization", "Bearer <token>")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.moonshot.ai/v1/batches/{batch_id}/cancel")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
response = http.request(request)
puts response.read_body{
"id": "<string>",
"object": "batch",
"endpoint": "<string>",
"input_file_id": "<string>",
"completion_window": "<string>",
"created_at": 123,
"request_counts": {
"completed": 123,
"failed": 123,
"total": 123
},
"output_file_id": "<string>",
"error_file_id": "<string>",
"in_progress_at": 123,
"expires_at": 123,
"finalizing_at": 123,
"completed_at": 123,
"failed_at": 123,
"cancelling_at": 123,
"cancelled_at": 123,
"metadata": {}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}{
"error": {
"message": "<string>",
"type": "<string>",
"code": "<string>"
}
}cancelling and then to cancelled. Only tasks in validating, in_progress, or finalizing status can be cancelled.
Usage Example
Usage Example
import os
from openai import OpenAI
from openai.types import Batch
client = OpenAI(
api_key=os.environ.get("MOONSHOT_API_KEY"),
base_url=os.environ.get("MOONSHOT_BASE_URL", "https://api.moonshot.ai/v1"),
)
batch: Batch = client.batches.cancel("your_batch_id")
print(f"Status: {batch.status}") # cancelling
curl -X POST ${MOONSHOT_BASE_URL:-https://api.moonshot.ai/v1}/batches/your_batch_id/cancel \
-H "Authorization: Bearer $MOONSHOT_API_KEY"
const OpenAI = require("openai");
const client = new OpenAI({
apiKey: process.env.MOONSHOT_API_KEY,
baseURL: process.env.MOONSHOT_BASE_URL || "https://api.moonshot.ai/v1",
});
async function main() {
const batch = await client.batches.cancel("your_batch_id");
console.log(`Status: ${batch.status}`); // cancelling
}
main();
Response Fields
Response Fields
BatchObject containing the following fields:| Field | Type | Description |
|---|---|---|
id | string | Unique identifier for the batch task |
object | string | Object type, always batch |
endpoint | string | Request endpoint |
input_file_id | string | Input file ID |
completion_window | string | Processing time window |
status | string | Current status. After a successful cancellation request this is typically cancelling, and eventually becomes cancelled |
output_file_id | string | null | Output file ID for successful results |
error_file_id | string | null | Error file ID for failed results |
created_at | integer | Creation timestamp (Unix) |
in_progress_at | integer | null | Execution start timestamp (Unix) |
expires_at | integer | null | Expiration timestamp (Unix) |
finalizing_at | integer | null | Result preparation start timestamp (Unix) |
completed_at | integer | null | Completion timestamp (Unix) |
failed_at | integer | null | Validation failure timestamp (Unix) |
cancelling_at | integer | null | Cancellation request timestamp (Unix) |
cancelled_at | integer | null | Cancellation completion timestamp (Unix) |
request_counts | object | Request counts, including completed, failed, and total |
metadata | object | null | Custom metadata |
validating, in_progress, or finalizing status can be cancelled. If the task is already completed, failed, expired, or cancelled, calling this endpoint will return a 400 error.- 400 Bad Request: The task status does not allow cancellation, or the request parameters are invalid. Please verify the task status before attempting cancellation.
- 401 Unauthorized: Invalid or missing API key. Check that
Authorization: Bearer <key>is correct. - 404 Not Found: The specified
batch_iddoes not exist. Verify the ID is correct and the task belongs to your organization. - 500 Server Error: Internal server error. Please retry later; if the issue persists, contact support with the
request_id.
Authorizations
The Authorization header expects a Bearer token. Use an MOONSHOT_API_KEY as the token. This is a server-side secret key. Generate one on the API keys page in your dashboard.
Path Parameters
The ID of the batch task
Response
The cancelled batch task
Unique identifier for the batch task
Object type, always batch
"batch"
Request endpoint
Input file ID
Processing time window
Current status: validating, failed, in_progress, finalizing, completed, expired, cancelling, cancelled
validating, failed, in_progress, finalizing, completed, expired, cancelling, cancelled Creation timestamp (Unix)
Show child attributes
Show child attributes
Output file ID for successful results
Error file ID for failed results
Execution start timestamp (Unix)
Expiration timestamp (Unix)
Result preparation start timestamp (Unix)
Completion timestamp (Unix)
Validation failure timestamp (Unix)
Cancellation request timestamp (Unix)
Cancellation completion timestamp (Unix)
Custom metadata
Show child attributes
Show child attributes
Was this page helpful?