REST Admin API returns 200 OK for New Draft Order

Fowlertown_Web_
Shopify Partner
18 1 3

Hey there, I'm a newbie with Shopify API (and APIs in general) so please be gentle. I have a development store where I'm testing a my first private app that will create a draft order for a customer who has products in their cart.

But I'm not even that far yet, I'm just trying to submit a new draft order successfully with manually-entered JSON data that I copy/pasted from the reference docs, and the result I'm getting is a 200 OK response status, instead of the 201 Created response that Shopify API Docs gives in its example. https://shopify.dev/docs/admin-api/rest/reference/orders/draftorder#create-2020-10

In addition to the 200 OK response status, my result object is undefined.

I'm using a basic JS call with `fetch(url,json)` to POST to the `/admin/api/2020-10/draft_orders.json` endpoint.

My console code is below. Can anyone spot what I'm doing wrong?

 

var obj = {
  return {
    "draft_order": {
      "line_items": [
        {
          "id": 6121005744299,
          "title": "Fancy Pants",
          "price": "9.99",
          "quantity": 2
        }
      ],
      "customer": {
        "id": 4508630712491
      },
      "use_customer_default_address": true
    }
  };
}

var url = '/admin/api/2020-10/draft_orders.json'

var fetchBody = () => {
  return {
    'method': 'POST',
    'headers': {
      'X-Shopify-Storefront-Access-Token': '4c83ecf984755b699086217a8dc40e76',
      'Content-Type': 'application/json'
    },
    'body': JSON.stringify(obj)
  };
}

console.log(url,obj,JSON.stringify(obj))

fetch(url,fetchBody())
.then(response => {
  console.log(response)
})
.then(result => {
  console.log('result',result)
})

 

 

 

 

 

 

 

0 Likes
Fowlertown_Web_
Shopify Partner
18 1 3

I switched to GraphQL and got the same result.

Using GraphQL endpoint with JS `fetch()` to post with `"Content-Type": "application/graphql"` in the headers, I'm still getting a 200 OK response but not 201 Created, and the result is `undefined` with no draft order getting created. I'm running out of ideas to try and troubleshoot this!

My updated code, which I modified from http://www.codeshopify.com/blog_posts/the-easy-way-to-do-the-shopify-storefront-api-graphql-with-fet...

var draftOrderMutation = () => `
mutation {
  draftOrderCreate(
  	input: {
      customerId: "gid://shopify/Customer/4508630712491",
      lineItems: [
        {
          variantId: "gid://shopify/Product/37734040436907",
          quantity: 2
        }
      ]
    }
  )
  {
    draftOrder {
      id
    }
    userErrors {
      field
      message
    }
  }
}
`;

var STOREFRONT_ACCESS_TOKEN = '4c83ecf984755b699086217a8dc40e76'
var GRAPHQL_URL = '/api/2020-10/graphql.json'

var GRAPHQL_BODY  = () => {
	return {
	'async': true,
	'crossDomain': true,
	'method': 'POST',
	'headers': {
		'X-Shopify-Storefront-Access-Token': STOREFRONT_ACCESS_TOKEN,
		'Content-Type': 'application/graphql',
	},
	'body': draftOrderMutation()
	};
}

fetch(GRAPHQL_URL, GRAPHQL_BODY())
  .then(response => {
		console.log('response',response)
  })
	.then(result => {
		console.log('result', result)
	})

 

And here's the response output in the console:

Fowlertown_Web__1-1607790930646.png

0 Likes
Greg_Kujawa
Shopify Partner
1016 83 243

This thread is a few years old, but I wonder if it still applies?

https://community.shopify.com/c/Shopify-APIs-SDKs/How-to-place-a-draft-order-using-storefront-api/td....

 

0 Likes
Fowlertown_Web_
Shopify Partner
18 1 3

And using the GraphiQL App by Shopify to test my GraphQL mutation, I get "Variables are invalid JSON: JSON.parse: expected double-quoted property name at line 4 column 1 of the JSON data." as the result, which I don't know how to interpret.

Fowlertown_Web__0-1607791212640.png

 

0 Likes
Greg_Kujawa
Shopify Partner
1016 83 243

Could be a quirk in how the GraphiQL app works. Maybe try defining the input under the Query Variables pane at the bottom...

0 Likes
elmorgan3
Tourist
6 0 2

Can I ask you where or when are you trying to create this Draft Order? I mean, are you using a webhook, to get here or any kind of event?

0 Likes