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.

Re: Using cURL Command To Add Metaobject Entries (GraphQL Admin API)

Solved

Using cURL Command To Add Metaobject Entries (GraphQL Admin API)

arieswen415
Shopify Partner
21 5 3

Hi Shopify Community,

 

I would like to add Metaobject entries using GraphQL Admin API, but I'm currently facing an issue of making API queries directly from cmd (on Windows 11).

Could anyone help me with this problem? What am I doing wrong? Any input is appreciated!


Here's the test command I entered:

# In cmd
curl -X POST https://shop-name.myshopify.com/admin/api/2023-04/graphql.json -H 'Content-Type: application/graphql' -H 'X-Shopify-Access-Token: my-access-token' -d '{products(first:3){edges{node{id}}}}'

Which is the example I got from GraphQL Admin API reference.

 

Here's the errors I got:

{"errors":{"query":"Required parameter missing or invalid"}}
curl: (6) Could not resolve host: application
curl: (3) URL using bad/illegal format or missing URL
 

Some other things I've tried are:

1.    Make sure that the URL and the access token are correct and all the access scopes are selected.

2.    Change the format of the query

Instead of this:

-d '{products(first:3){edges{node{id}}}}'
I did this:
-d '{"query":"{products(first:3){edges{node{id}}}}"}'
 

 

However, it is still giving me the same errors.

Everything above is only a simple command that I wanted to try out before I actually perform the mutation.

 

Here's my actual query for adding a Metaobject entry:

#Everything here should be in one single line. Showing multiple lines are only for higher readability

curl -X POST https://shop-name.myshopify.com/admin/api/2023-04/graphql.json
-H 'Content-Type: application/graphql'
-H 'X-Shopify-Access-Token: my-access-token'
-d '{
   "query":"mutation {
      metaobjectCreate(metaobject:{
         type:\"metaobject-type\",
         capabilities:{
            publishable:{
               status:ACTIVE
            }
         }
         fields:[
            {
               key:\"display_name\",
               value:\"Test Entry\"
            },
            {
               key:\"color\",
               value:\"White\"
            }
         ]
      }) {
         metaobject{
            id
         }
      }
   }"
}
'

This gives me the same exact error, but I'm not sure what I did wrong here.

 

Any help is appreciated!

Thank you for spending time reading this.

Accepted Solution (1)

arieswen415
Shopify Partner
21 5 3

This is an accepted solution.

Here's the final command and query that works for me

 

curl -H "Content-Type: application/graphql" -H "X-Shopify-Access-Token: my-access-token" -d "mutation {metaobjectCreate (metaobject: {type: \"chassis\",capabilities: {publishable: {status: ACTIVE}}fields: [{key: \"display_name\", value: \"Test Entry\" }, {key: \"color\", value: \"White\" }]}) {metaobject {id}}}" https://shop-name.myshopify.com/admin/api/2023-04/graphql.json

 

 

I changed all single quotes to double quotes since cmd doesn't treat single quotes as anything but a regular character, and I did not type the word "query" in the -d part of the command.

 

 

View solution in original post

Replies 7 (7)

ShopifyDevSup
Shopify Staff
1453 238 524

Hi @arieswen415 👋

 

The endpoint needs to be surrounded by single quotes: 'https://SHOP_NAME.myshopify.com/admin/api/2023-04/graphql.json'

 

Given the "curl: (6) Could not resolve host: application" error, would you please try removing the space between "Content-Type:" and "application/graphql" (i.e. 'Content-Type:application/graphql') as suggested here. There are several other suggestions there as well.

 

From my testing, the below works on OSX:

 

curl -L 'https://SHOP_NAME.myshopify.com/admin/api/2023-04/graphql.json' \
-H 'Content-Type:application/json' \
-H 'X-Shopify-Access-Token:ACCESS_TOKEN' \
-d '{"query":"mutation { metaobjectCreate(metaobject:{ capabilities: { publishable: { status: ACTIVE } }, type: \"$app:SOME_METAOBJ_DEF\", fields: { key: \"product\", value: \"gid://shopify/Product/123\" } }) { metaobject { id type field(key: \"product\") { value } } } } "}'


 

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

arieswen415
Shopify Partner
21 5 3

Thank you for you reply! I've tried the methods you provide, and the error of curl: (6) Could not resolve host: application has been resolved! Very appreciate it!

However, I'm still getting some errors.

 

I added the single quotes and removed the spaces

# With single quotes; with no spaces
curl -X POST 'https://shop-name.myshopify.com/admin/api/2023-03/graphql.json' -H 'Content-Type:application/graphql' -H 'X-Shopify-Access-Token:my-access-token' -d '{"query":"{products(first:3){edges{node{id}}}}"}'

Here's what I got

curl: (3) URL using bad/illegal format or missing URL

 

Therefore, I remove the single quote to test it out

# With no single quotes or spaces
curl -X POST https://shop-name.myshopify.com/admin/api/2023-03/graphql.json -H 'Content-Type:application/graphql' -H 'X-Shopify-Access-Token:my-access-token' -d '{"query":"{products(first:3){edges{node{id}}}}"}'

Here's what I got

{"errors":{"query":"Required parameter missing or invalid"}}

 

I'm assuming the error is caused by invalid query, but I'm not sure exactly. If you could tell me how to fix this issue, that would be great! Thank you again for your help!

ShopifyDevSup
Shopify Staff
1453 238 524

Hi @arieswen415,

 

Looking back at the original post where you mentioned that the most basic product query wasn't working, using that first test command for a few product id's works normally here as provided just copying and pasting and filling in the missing info. 

 

This indicates that there's something different about either your version of curl or the system it's running on leading to the unexpected behaviour so taking a closer look at that is recommended - this is beyond our direct scope of support so perhaps the community will have further ideas. To give you some frame of reference the system here being tested on is using curl 7.88.1 released Feb. 20, 2023. 

 

As far as format of the query, [the official GraphQL API doc example for curl] does not include the "query" part at the beginning, like that initial example you shared, which seems like a good base to continue troubleshooting from.

 

Hope you have a great day,
Jon551

 

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

arieswen415
Shopify Partner
21 5 3

Hi, thank you so much for your reply.

Here's the cURL version I have on Windows 11:

image.png

 

Thank you for providing some clues to test out.

I've tried to write a simpler query without "query" but still gotten the same thing

 

curl -X POST https://shop-name.myshopify.com/admin/api/2023-04/graphql.json -H 'Content-Type:application/json' -H 'X-Shopify-Access-Token:my-access-token' -d '{shop{name}}'

 

 

Is there any further documentation regarding writing GraphQL query in cURL that I can look up since Shopify Official Documentation does not work?

Or is this caused by the os that I'm using? Please also let me know if there is any dependencies that I need to install, such as Node.js or Ruby, in the fist place.

 

Thank you again for your help!

ShopifyDevSup
Shopify Staff
1453 238 524

Hey @arieswen415 - thanks for reaching out again. I'm not too familiar with using cURL within a Windows environment (my primary environment is Mac), but I did test out the query you provided above and it worked for me (I swapped out the generic app/store credentials for my own and added a basic GraphQL query). This is very odd - it seems like it may be an issue with the specific version of cURL you're using as mentioned before. 

Our team mainly focuses on Shopify-specific API/Development questions, so for third party libraries and tools, we're not able to assist too much, but the cURL repository on Github might be a good place to reach out. I would open up an issue there if you haven't already - this would get you in touch with the cURL developers directly as well as other users of cURL who might be able to help. Since this isn't our repository or tool, I can't guarantee anything on our end, but just wanted to share that as a potential place to look for some more help here. 

Hope this helps and the issue gets resolved for you soon. 

Al | Shopify Developer Support

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

arieswen415
Shopify Partner
21 5 3

Hi @ShopifyDevSup 

 

Thanks for the follow up! I really appreciate your help thus far.

I understand that this may fall out of the scope your field. At least now I know that the problem is not caused by the query but possibly by the device settings or cURL version.

 

I'll open an issue in the repo and continue over there. Thank you for providing this option. Really appreciate your time and all the information you provided!

arieswen415
Shopify Partner
21 5 3

This is an accepted solution.

Here's the final command and query that works for me

 

curl -H "Content-Type: application/graphql" -H "X-Shopify-Access-Token: my-access-token" -d "mutation {metaobjectCreate (metaobject: {type: \"chassis\",capabilities: {publishable: {status: ACTIVE}}fields: [{key: \"display_name\", value: \"Test Entry\" }, {key: \"color\", value: \"White\" }]}) {metaobject {id}}}" https://shop-name.myshopify.com/admin/api/2023-04/graphql.json

 

 

I changed all single quotes to double quotes since cmd doesn't treat single quotes as anything but a regular character, and I did not type the word "query" in the -d part of the command.