A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
Hello, for the past 2 days I'm trying to write a python script which would automatically populate a new order metafield on shopify with a predefined value.
So far, I've conquered getting the metafield ID, which I now understand is unique for every single order. (yay)
I'm having trouble actually populating this metafield on a new/old order with the information I need.
I'm trying to perform a POST request with headers content type: application/json.
My authentication is in the URL and the payload is the metafield data.
payload={"metafields":[ { "id":23069051224283, "namespace":"custom", "key":"wholecell_url", "value":"https:\/\/www.wholecell.io\/orders\/*value*", "owner_id":5011527106779}]} headers = { "Content-Type": "application/json", } r = requests.post(url, headers=headers, data=payload)
With this I get a response 400.
Solved! Go to the solution
This is an accepted solution.
Your payload is a dict and needs to be encoded into a JSON format. You'll need something like this instead:
import requests
import json
url = f"https://{shop_name}.myshopify.com/admin/api/2022-10/orders/{order_id}/metafields.json"
headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": token
}
payload = json.dumps({
"metafield": {
"namespace": "custom",
"key": "test",
"type": "url",
"value": "https://shopify.dev/"
}
})
requests.post(url, headers=headers, data=payload)
Hope that 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
Hi @tuksnesis👋
To use the Admin API, you'll want to send authenticated requests by including the following in your header: `{ X-Shopify-Access-Token: YOUR_ADMIN_ACCESS_TOKEN }`. I'd recommend reviewing this page on different types of authentication/authorization methods depending on your app type. If you're working with a custom app created in the Admin, you can use this guide to get and use access tokens.
Hope that 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
We've created a custom private app already, although I don't see an option to get access tokens. I can see our API key and secret + the shared secret.
As noted in the docs here, the token can be revealed at most once, so you should still be able to see the token since you haven't used any tokens yet. Please follow the doc and navigate to the appropriate page where you can find the token.
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
I read through the doc and it seems to not match our use case. I can't get one single access token.
Our authentication key is in the url, with that I can do GET requests. I can request metafield info for a specific order, but why doesn't it work with POST? Makes no sense at all. I'm making an authenticated request.
For custom apps created in the Admin, we recommend using the access token in the request header as the `X-Shopify-Access-Token` . It should look like the below in your Admin > Settings > Apps and sales channels > Develop apps > YOUR-APP page. If there isn't an option to reveal the token, you can also rotate the API credentials by uninstalling/re-installing the app.
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
I've gotten my hand on the access token. Still Response 400.
import requests
url = "https://ourshop.myshopify.com/admin/api/2022-10/orders/**orderid**/metafields.json"
payload={"metafields":[
{
"namespace":"custom",
"key":"wholecell_url",
"type":"url",
"value":"**my url value**"
}]}
headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": "**here is my access token**"
}
r = requests.post(url, headers=headers, data=payload)
print(r)
This is an accepted solution.
Your payload is a dict and needs to be encoded into a JSON format. You'll need something like this instead:
import requests
import json
url = f"https://{shop_name}.myshopify.com/admin/api/2022-10/orders/{order_id}/metafields.json"
headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": token
}
payload = json.dumps({
"metafield": {
"namespace": "custom",
"key": "test",
"type": "url",
"value": "https://shopify.dev/"
}
})
requests.post(url, headers=headers, data=payload)
Hope that 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