> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/umbraco-commerce/how-to-guides/create-order-via-code.md).

# Create an Order via Code

In some cases, such as when importing orders from another system, it may be necessary to create a fully finalized order via code.

The example below demonstrates how to do this. The `api` variable is an instance of the `IUmbracoCommerceApi` interface, which is an injectable service accessed via [dependency injection](/umbraco-commerce/key-concepts/dependency-injection.md).

```csharp
await api.Uow.ExecuteAsync(async (uow) =>
{
    // Fetch the store
    var store = await api.GetStoreAsync("blendid");
    
    // Create or get the current order
    var order = await api.GetOrCreateCurrentOrderAsync(store.Id).AsWritableAsync(uow);

    // Add product
    var productId = "2fa949e4-2acb-4aef-bb7c-4d3a5f7bbe52"; // The product node Key
    await order.AddProductAsync(productId, null, 1);

    // Set customer details
    var country = await api.GetCountryAsync(store.Id, "GB");
    await order.SetPropertiesAsync(new Dictionary<string, string>
        {
            { Constants.Properties.Customer.EmailPropertyAlias, $"customer@example.com" },
            { "marketingOptIn", "0" },
            { Constants.Properties.Customer.FirstNamePropertyAlias, "Example" },
            { Constants.Properties.Customer.LastNamePropertyAlias, $"Customer" },
            { "billingAddressLine1", "10 Example Road" },
            { "billingCity", "Example City" },
            { "billingZipCode", "EX3 3PL" },
            { "billingTelephone", "0123456789" },
            { "shippingSameAsBilling", "1" }
        })
        .SetPaymentCountryRegionAsync(country, null)
        .SetShippingCountryRegionAsync(country, null);

    // Set shipping method
    var shippingMethod = await api.GetShippingMethodAsync(store.Id, "pickup");
    await order.SetShippingMethodAsync(shippingMethod);

    // Set payment method
    var paymentMethod = await api.GetPaymentMethodAsync(store.Id, "invoicing");
    await order.SetPaymentMethodAsync(paymentMethod);

    // Recalculate order
    await order.RecalculateAsync();

    // Finalize order
    await order.InitializeTransactionAsync();
    await order.FinalizeAsync(order.TransactionAmount.Value, Guid.NewGuid().ToString("N"), PaymentStatus.Authorized);

    // Save order
    await api.SaveOrderAsync(order);

    // Commit the unit of work
    uow.Complete();
});
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.umbraco.com/umbraco-commerce/how-to-guides/create-order-via-code.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
