A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
Hi, I'm trying to create a product using Python, the requests library, and Shopify's API. Below is my code. It seems to return ok, but a product isn't being created in my inventory. I'm fairly new to this so any assistance, or if there is a better way, it would be greatly appreciated.
shopify_token = getShopifyCredentials()
headers = {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': shopify_token
}
product_inputs = {"product": {
"title": "Test API product creation",
"body_html": "<strong>Good product!</strong>",
"vendor": "Some Vendor",
"product_type": "Product Type",
"status": "draft"
}
}
endpoint = shop_url+'/admin/api/2021-04/products.json'
response = requests.post(endpoint, headers=headers, json=product_inputs)
print(response)
I'n getting a Response [200] code.
Solved! Go to the solution
This is an accepted solution.
Solved! When I received the error, it showed the call being redirected to another URL than the mystorefront URL. I replaced my storefront URL with this new URL in my script and it worked correctly, giving a 201 response and creating the product!
Hi - you may have more success if you use the Shopify Python library, which supports the GraphQL Admin API, rather than the REST API.
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me 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
Hi Liam,
Thanks for the suggestion! I've installed the Shopify Python module and can set up a session. However, I still need help to create a product.
def startShopifySession():
'''session setup'''
api_version = '2022-07'
private_app_password = getShopifyToken()
session = shopify.Session(shop_url, api_version, private_app_password)
shopify.ShopifyResource.activate_session(session)
return session
def endShopifySession():
shopify.ShopifyResource.clear_session()
session = startShopifySession()
product = shopify.Product()
product.title = "Test Product"
product.save()
endShopifySession()
I'm not receiving any errors, but I'm also not seeing any products being created in my inventory.
So I tried something new. I used the Shopify module to find an existing product. It does so successfully.
However, when I try to make changes and save the product, I'm getting an error code.
session = startShopifySession()
product = shopify.Product.find(9468056895783)
pprint(product.attributes)
endShopifySession()
{'admin_graphql_api_id': 'gid://shopify/Product/9468056895783',
'body_html': '',
'created_at': '2024-07-19T12:12:00-04:00',
'handle': 'a',
'id': 9468056895783,
'image': None,
'images': [],
'options': [option(11876717461799)],
'product_type': '',
'published_at': None,
'published_scope': 'web',
'status': 'draft',
'tags': '',
'template_suffix': '',
'title': 'a',
'updated_at': '2024-07-19T12:28:26-04:00',
'variants': [variant(48860058648871)],
'vendor': 'Vendor'}
Process finished with exit code 0
Now when changing the product I get an error:
session = startShopifySession()
product = shopify.Product.find(9468056895783)
product.price = "9.99"
product.save()
pprint(product.attributes)
endShopifySession()
pyactiveresource.connection.Redirection: Response(code=301, body="b''", headers={'Date': 'Fri, 19 Jul 2024 18:39:19 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'close', 'X-Sorting-Hat-PodId': '294', 'X-Sorting-Hat-ShopId': '80191488295', 'referrer-policy': 'origin-when-cross-origin', 'x-frame-options': 'DENY', 'x-shopid': '80191488295', 'x-shardid': '294', 'location': 'https://711923-2.myshopify.com/admin/api/2022-07/products/9468056895783.json', 'strict-transport-security': 'max-age=7889238', 'set-cookie': 'request_method=PUT; path=/; SameSite=Lax', 'x-request-id': 'e8adfc04-1fbe-471e-b8c6-4ff2ba5c18b1-1721414359', 'server-timing': 'processing;dur=34, socket_queue;dur=3.042, edge;dur=2.045, util;dur=0.111, memcache;dur=6.135, redis;dur=0.0, db;dur=1.712, elasticsearch;dur=0.0, view;dur=0.0, externals;dur=7.847, graphql_lex;desc="GraphQL Lexing";dur=0.0, graphql_parse;desc="GraphQL Parsing";dur=0.0, graphql_execute;desc="GraphQL Execution";dur=0.0, graphql_analyze;desc="GraphQL Analysis";dur=0.0, graphql_validate;desc="GraphQL Static Validation";dur=0.0', 'server-timing-public': 'processing;dur=34', 'content-security-policy': "default-src 'self' data: blob: 'unsafe-inline' 'unsafe-eval' https://* shopify-pos://*; block-all-mixed-content; child-src 'self' https://* shopify-pos://*; connect-src 'self' wss://* https://*; frame-ancestors 'none'; img-src 'self' data: blob: https:; script-src https://cdn.shopify.com https://cdn.shopifycdn.net https://checkout.shopifycs.com https://api.stripe.com https://mpsnare.iesnare.com https://appcenter.intuit.com https://www.paypal.com https://js.braintreegateway.com https://c.paypal.com https://maps.googleapis.com https://www.google-analytics.com https://v.shopify.com 'self' 'unsafe-inline' 'unsafe-eval'; upgrade-insecure-requests; report-uri /csp-report?source%5Baction%5D=update&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=e8adfc04-1fbe-471e-b8c6-4ff2ba5c18b1-1721414359", 'x-content-type-options': 'nosniff', 'x-download-options': 'noopen', 'x-permitted-cross-domain-policies': 'none', 'x-xss-protection': '1; mode=block; report=/xss-report?source%5Baction%5D=update&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin_api&source%5Buuid%5D=e8adfc04-1fbe-471e-b8c6-4ff2ba5c18b1-1721414359', 'x-dc': 'gcp-us-east4,gcp-us-central1,gcp-us-central1', 'CF-Cache-Status': 'DYNAMIC', 'Report-To': '{"endpoints":[{"url":"https:\\/\\/a.nel.cloudflare.com\\/report\\/v4?s=M5sw4C2u34o7gMg2tcbhAchfWcXCP0L0gTnCTh0oNkiz81%2FvFot8koSMcY%2FWrtxCSa5%2FMECPo3vKTaVGkOZp9bnD4RhnuOCmxNNIuxZf1s8N9qu3rrArnFbjgbiSu5q5Jx0vRn5KuKwytvC5K%2BU%3D"}],"group":"cf-nel","max_age":604800}', 'NEL': '{"success_fraction":0.01,"report_to":"cf-nel","max_age":604800}', 'Server-Timing': 'processing;dur=34, socket_queue;dur=3.042, edge;dur=2.045, util;dur=0.111, memcache;dur=6.135, redis;dur=0.0, db;dur=1.712, elasticsearch;dur=0.0, view;dur=0.0, externals;dur=7.847, graphql_lex;desc="GraphQL Lexing";dur=0.0, graphql_parse;desc="GraphQL Parsing";dur=0.0, graphql_execute;desc="GraphQL Execution";dur=0.0, graphql_analyze;desc="GraphQL Analysis";dur=0.0, graphql_validate;desc="GraphQL Static Validation";dur=0.0', 'Server': 'cloudflare', 'CF-RAY': '8a5cd56108731438-PIT', 'alt-svc': 'h3=":443"; ma=86400'}, msg="Moved Permanently")
This is an accepted solution.
Solved! When I received the error, it showed the call being redirected to another URL than the mystorefront URL. I replaced my storefront URL with this new URL in my script and it worked correctly, giving a 201 response and creating the product!