Development discussions around Shopify APIs
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)
})
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:
This thread is a few years old, but I wonder if it still applies?
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.
Could be a quirk in how the GraphiQL app works. Maybe try defining the input under the Query Variables pane at the bottom...
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?
User | RANK |
---|---|
8 | |
7 | |
3 | |
3 | |
3 |
Connect your PayPal account to allow your customers to checkout using the PayPal gateway a...
ByYour online store speed can enhance your store’s discoverability, boost conversion rates a...
ByShopping is at our fingertips with mobile devices. Is your theme optimized to be user-frie...
By