Create a cart
POST
https://api.testingapis.com/api/CartsRequest body
Optional. application/json, text/json, application/*+json, application/xml, text/xml, application/*+xml. Model: Cart. The new cart. Any client-supplied ID is replaced.
Example body
{"userId": 103, "items": [{"productId": 1, "quantity": 2, "price": 249.99}]}Responses
- 200
- OK
- 201
- Created; returns { message, data } with the generated ID.
- 400
- The request body is missing.
- 500
- Unexpected server error.
Example
Request
curl -X POST "https://api.testingapis.com/api/Carts" \
-H "Content-Type: application/json" \
-d '{"userId": 103, "items": [{"productId": 1, "quantity": 2, "price": 249.99}]}'Response201 Created
{
"message": "Cart created successfully",
"data": {
"id": 12,
"userId": 103,
"items": [{"productId": 1, "quantity": 2, "price": 249.99}],
"total": 499.98,
"updatedAt": "2026-09-30T18:26:45Z"
}
}Code examples
JavaScript (fetch)
const response = await fetch("https://api.testingapis.com/api/Carts", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: "{\"userId\": 103, \"items\": [{\"productId\": 1, \"quantity\": 2, \"price\": 249.99}]}",
});
console.log(response.status, await response.text());Python (requests)
import requests
response = requests.request(
"POST",
"https://api.testingapis.com/api/Carts",
headers={
"Content-Type": "application/json",
},
data="{\"userId\": 103, \"items\": [{\"productId\": 1, \"quantity\": 2, \"price\": 249.99}]}".encode("utf-8"),
allow_redirects=False,
)
print(response.status_code, response.text)Java (HttpClient)
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 {
HttpClient client = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NEVER)
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.testingapis.com/api/Carts"))
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\"userId\": 103, \"items\": [{\"productId\": 1, \"quantity\": 2, \"price\": 249.99}]}"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
}
}