Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Unable to create orders by using POST /admin/api/2022-07/orders.json

Solved

Unable to create orders by using POST /admin/api/2022-07/orders.json

namansharma
New Member
6 0 0

I am trying to create a new order using the POST /admin/api/2022-07/orders.json API, but instead of giving the response 201 Created, the API is responding with the list of orders and Status code 200.

 

Here are the details:

URL = "https://XXXXXXX/admin/api/2022-07/orders.json"

method: POST

Headers = {
'X-Shopify-Access-Token': 'shppa_XXXXXXX',
'Content-Type': 'application/json',
}

Payload = 

{
"order": {
"line_items": [
{
"variant_id": 42858663051445,
"quantity": 1
}
],
"customer": {
"first_name": "John",
"last_name": "Doe",
"email": "john.doe@example.in"
},
"financial_status": "paid"
}
}
 
I have checked that I have got the necessary permission of read and write orders.
Accepted Solution (1)

javitek
Tourist
5 2 0

This is an accepted solution.

I found the solution. You probably use a custom domain name, let's say: "myshop.com" so in your request I imagine you use:
https://myshop.com/admin/api/2023-04/orders.json

That's the mistake. Unfortunately postman don't warn us about that but the request actually is returning a 301 redirect. So apparently the default response after the redirect is a GET. I don't know why. The way I noticed the error was by executing the request via CURL in the terminal.

 

Do the POST request in the terminal and you will see something like this:

curl -i -X POST -H "Content-Type: application/json" -H "X-Shopify-Access-Token: " -d '{"order":{"line_items":[{"variant_id": ,"quantity":1}],"customer":{"id": },"financial_status":"pending"}}' https://myshop.com/admin/api/2023-04/orders.json

HTTP/2 301 

date: Fri, 07 Jul 2023 03:15:44 GMT

content-type: text/html; charset=utf-8

location: https://realdomain.myshopify.com/admin/api/2023-04/orders.json

....

 

So replace "myshop.com" with "realdomain.myshopify.com" and it will work!!

 

Happy coding.

 

View solution in original post

Replies 8 (8)

ShopifyDevSup
Shopify Staff
1453 238 525

Hi @namansharma , you should follow the steps here https://shopify.dev/docs/api/admin-rest/2023-04/resources/order#post-orders, using a POST to that endpoint with a json content type should create an Order. The results you are describing seem to suggest it is a GET request that is being made. Perhaps you can test this in a client like Postman. 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

namansharma
New Member
6 0 0

Hello!

 

Thanks for the response but I am doing exactly the same as described in https://shopify.dev/docs/api/admin-rest/2022-07/resources/order#post-orders but the response is not ideal.

 

Here is the postman screenshot showing it is a a POST request and the response received with status code 200 and a list of orders.

Screenshot 2023-06-23 at 4.33.48 PM.png

 

Kindly, let me know if there is anything that I am missing.

Cheers!

ShopifyDevSup
Shopify Staff
1453 238 525

Hi @namansharma , the screenshot is very small, but the endpoint seems correct. We would advise first trying to create a simple test order with minimal Body details using the API as per the example in the curl docs here. Remember to save in Postman and once you have that working try adding more details to the body until it matches your requirements or reveals an error. Alternatively if you continue to be stuck with this you can reach out to our Support team with the x-request-id from the response headers and we can investigate in our logs with some more detail. Hope this helps!

 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

javitek
Tourist
5 2 0

Hi there, did you find the solution? I have the same problem. Please let know

namansharma
New Member
6 0 0

No.
I did what was suggested to me in the previous post. I contacted the support team with the X -Request Id and other details. However, I am yet to receive a resolution for the same as the case is been looked into.

 

javitek
Tourist
5 2 0

I also contacted support. But they haven't told me a solution yet. It doesn't make sense that the request responses with status 200. The documentation doesn't mention anything about that.

javitek
Tourist
5 2 0

This is an accepted solution.

I found the solution. You probably use a custom domain name, let's say: "myshop.com" so in your request I imagine you use:
https://myshop.com/admin/api/2023-04/orders.json

That's the mistake. Unfortunately postman don't warn us about that but the request actually is returning a 301 redirect. So apparently the default response after the redirect is a GET. I don't know why. The way I noticed the error was by executing the request via CURL in the terminal.

 

Do the POST request in the terminal and you will see something like this:

curl -i -X POST -H "Content-Type: application/json" -H "X-Shopify-Access-Token: " -d '{"order":{"line_items":[{"variant_id": ,"quantity":1}],"customer":{"id": },"financial_status":"pending"}}' https://myshop.com/admin/api/2023-04/orders.json

HTTP/2 301 

date: Fri, 07 Jul 2023 03:15:44 GMT

content-type: text/html; charset=utf-8

location: https://realdomain.myshopify.com/admin/api/2023-04/orders.json

....

 

So replace "myshop.com" with "realdomain.myshopify.com" and it will work!!

 

Happy coding.

 

namansharma
New Member
6 0 0

Thanks. It worked. 😄