---
title: "Work with the Orders API (C#)"
slug: "work-with-the-orders-api--c--"
abstract: "How to use the Sitecore XC Orders API to integrate with other business systems to exchange order-related data."
---
# Work with the Orders API (C#)

The Orders service provides a RESTful, OData-compliant API, that you can use to integrate with other business systems to exchange order-related data. This topic provides examples of how you can use the Orders API to build Commerce integrations using the C# programming language.

> [!note]
> The C# code examples contained in this topic are based on the `Sitecore.Commerce.Sample.Console` samples that are packaged in the [Sitecore Commerce Engine Software Development Kit](https://doc.sitecore.com/xp/en/developers/103/sitecore-experience-commerce/compile-the-sample-commerce-engine.html "Compile the sample Commerce Engine") (`Sitecore.Commerce.Engine.SDK`).

You must include Commerce Engine operations and commands within a container call (using C#) as shown in this [example](https://doc.sitecore.com/xp/en/developers/103/sitecore-experience-commerce/build-a-container--c--.html "Build a container (C#)").

To execute any operation in the Sitecore Experience Commerce (XC) system, the calling system must first [obtain a valid bearer authorization token](https://doc.sitecore.com/xp/en/developers/103/sitecore-experience-commerce/obtain-an-authentication-token--c--.html "Obtain an authentication token (C#)") from the Sitecore Identity server.

> [!note]
> You can perform other operations programmatically, for example to work with orders fulfillment or order payments, by using the [Orders Views and Actions API](https://doc.sitecore.com/xp/en/developers/103/sitecore-experience-commerce/work-with-the-orders-entity-views-and-actions-api--c--.html "Work with the Orders Entity Views and Actions API (C#)") .

<details>
<summary><h2>Create an order (C#)</h2></summary>

You can use the Orders API to create an order when the shopping cart has all necessary items, fulfillment, and payment information.

To create an order, use the `CreateOrder` query. The query must provide the shopping cart ID (`cartId`) and the shopper's email address, for example:

```csharp
Container container = AuthoringContainer();
DataServiceActionQuerySingle<CommerceCommand> actionQuery = container.CreateOrder("cartId", "user@email.com");
CommerceCommand command = Proxy.DoCommand(actionQuery);
string orderId = command.Models.OfType<CreatedOrder>().FirstOrDefault().OrderId;

```

</details>

<details>
<summary><h2>Cancel an order (C#)</h2></summary>

You can use the Orders API to cancel an order when the order status is either *pending* or *on hold*.

To cancel an order:

```csharp
Container container = AuthoringContainer();
DataServiceActionQuerySingle<CommerceCommand> actionQuery = container.CancelOrder(orderId);
CommerceCommand command = Proxy.DoCommand(actionQuery);

```

</details>

<details>
<summary><h2>Set the order to a custom status (C#)</h2></summary>

You can set an order to a custom status.

> [!important]
> By default, the Commerce Orders flow is based on the orders status. The status of the order determines the next action to take on the order in the end-to-end process flow.  Keep in mind that the introduction of a custom order status impacts the way commerce orders normally flow through the system.

To set the order to a customer status using C#:

```csharp
DataServiceActionQuerySingle<CommerceCommand> actionQuery = container.SetOrderStatus(orderId, "CustomStatus");
CommerceCommand command = Proxy.DoCommand(actionQuery);

```

</details>

<details>
<summary><h2>Get sales activities from an order (C#)</h2></summary>

Sitecore XC generates sales activities every time an order's payment information is updated.

To retrieve the sales activities for an order using C#:

```

DataServiceQuery<SalesActivity> actionQuery = container.GetOrderSalesActivities(orderId);
IEnumerable<SaleActivity> salesActivities = Proxy.Execute(actionQuery);

```

</details>