How can I place an order using API from an extension?

How can I place an order using API from an extension?

vishal206
Shopify Partner
8 0 0

I want to make order from shopify extension. I used app proxy I got the data but now how can I place order or is it possible to call api directly on extension and make a order?

Replies 5 (5)

BrainStation23
Shopify Partner
406 60 58

hi @vishal206 ,
You can use shopify admin api to create order on the store. You can hit the create order API directly from the extension or you can have a dedicated route in you're app to create the order. The extension then should hit you're app and you're app with admin access should create the order on the store. To create a order you can refer to the shopifys API documentation

Create order - Shopify REST api docs 
Create a draft order using GraphQL admin api - Shopify 

Brain Station 23 PLC (Mail: [email protected])
- Was your question answered? Mark it as an Accepted Solution
- Did the solution not address your concern? We kindly request that share or mail your store URL with us this will enable us to collaborate more closely.
- Explore our Shopify public apps
vishal206
Shopify Partner
8 0 0

would you please show an example code? I have tried to use 

fetch("/admin/api/2023-10/orders.json") 

 but It shows 404 or 500 error

BrainStation23
Shopify Partner
406 60 58

hi @vishal206 

I see that your trying to use the Admin REST API to create an order. Create order is a post route and it needs a request body. The shape of the body is documented at the link I gave you before. I'm giving you a demo fetch request from the example from that link

const order = {};
order.line_items = [
  {
    "title": "Big Brown Bear Boots",
    "price": 74.99,
    "grams": "1300",
    "quantity": 3,
    "tax_lines": [
      {
        "price": 13.5,
        "rate": 0.06,
        "title": "State tax"
      }
    ]
  }
];
order.transactions = [
  {
    "kind": "sale",
    "status": "success",
    "amount": 238.47
  }
];
order.total_tax = 13.5;
order.currency = "EUR";

fetch("/admin/api/2023-10/orders.json", {
    method: "POST",
    body: JSON.stringify(order),
    headers: {
        "Content-type": "application/json"
    }
}) 
Brain Station 23 PLC (Mail: [email protected])
- Was your question answered? Mark it as an Accepted Solution
- Did the solution not address your concern? We kindly request that share or mail your store URL with us this will enable us to collaborate more closely.
- Explore our Shopify public apps
vishal206
Shopify Partner
8 0 0

one last thing should I call order API directly from extension or I use app proxy to get data from extension they I call order API using fetch inside app proxy route 

BrainStation23
Shopify Partner
406 60 58

hi @vishal206 ,

Maybe calling the API from you're app proxy is the better choice. 

Brain Station 23 PLC (Mail: [email protected])
- Was your question answered? Mark it as an Accepted Solution
- Did the solution not address your concern? We kindly request that share or mail your store URL with us this will enable us to collaborate more closely.
- Explore our Shopify public apps