Metafield created with Rest API, not appearing on Backend

Solved

Metafield created with Rest API, not appearing on Backend

RfcSilva
Shopify Partner
17 1 2

Hello

I have created several Metafields using the endpoint: 

https://{store_name}.myshopify.com/admin/api/2023-01/products/8485254594898/metafields.json.

 

With Payload:

{"metafield":
{"key":"event_level",
"value":"[\"Beginner\"]",
"type":"list.single_line_text_field",
"namespace":"custom",
"owner_id":8389892768037,
"owner_resource":"product",
"id":0,
"admin_graphql_api_id":""}
}

 

If I go to Postman and get all the metafields from the product, they all show up:

https://{store_name}.myshopify.com/admin/api/2023-01/products/8485254594898/metafields.json

 

    "metafields": [
        {
            "id": 39703382786386,
            "namespace": "custom",
            "key": "event_level",
            "value": "[\"Beginner\"]",
            "description": null,
            "owner_id": 8485254594898,
            "created_at": "2023-06-23T11:38:48+02:00",
            "updated_at": "2023-06-23T11:38:48+02:00",
            "owner_resource": "product",
            "type": "list.single_line_text_field",
            "admin_graphql_api_id": "gid://shopify/Metafield/39703382786386"
        },
        {
            "id": 39703382851922,
            "namespace": "event",
            "key": "access_control_needed_",
            "value": true,
            "description": null,
            "owner_id": 8485254594898,
            "created_at": "2023-06-23T11:38:48+02:00",
            "updated_at": "2023-06-23T11:38:48+02:00",
            "owner_resource": "product",
            "type": "boolean",
            "admin_graphql_api_id": "gid://shopify/Metafield/39703382851922"
        }, ....

 

But on Shopify backoffice, I can't see them on Settings>Custom data, and I can't also see them on the product page.

 

Settings>Custom Data

RfcSilva_1-1687877572868.png

 

Products

RfcSilva_2-1687877612050.png

 

Any clues?

 

Accepted Solution (1)
Liam
Shopify Staff
2873 312 820

This is an accepted solution.

It looks like you may need to create the metafield definition with the metafieldDefinitionCreate mutation first and then create the metafield as you previously did. Try this out and let me know if you're still running into issues. You could also create the definition on the admin and then make the API call you previously made. 

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

View solution in original post

Replies 6 (6)

Liam
Shopify Staff
2873 312 820

Hi RfcSilva,

 

On the admin panel from your first screenshot, if you click on the "Metafields without a definition" tab - are they appearing?

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

RfcSilva
Shopify Partner
17 1 2

Hi Liam,

Yes, they do in fact appear on "Metafields without a definition" tab, I haven't noticed it.

But considering that I have put all the information necessary, should't it create that structure when creating the Metafield?

 

 

 

Liam
Shopify Staff
2873 312 820

This is an accepted solution.

It looks like you may need to create the metafield definition with the metafieldDefinitionCreate mutation first and then create the metafield as you previously did. Try this out and let me know if you're still running into issues. You could also create the definition on the admin and then make the API call you previously made. 

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

RfcSilva
Shopify Partner
17 1 2

Hello Liam,

 

Thank you, I was able to create correctly the Metafield with the mutation CreateMetafieldDefinition in GraphQl.

But I am curious, what do you mean by "You could also create the definition on the admin"? What endpoint is this? Or is it also GraphQl?

I used this solution according to your instructions an it works (future reference):

 

Endpoint:
https://{{store_name}}.myshopify.com/admin/api/2023-01/graphql.json

 

Query:

mutation CreateMetafieldDefinition($definitionMetafieldDefinitionInput!) {
  metafieldDefinitionCreate(definition$definition) {
    createdDefinition {
        id
        name
        namespace
        key
        description
        ownerType
        type{name}
        metafieldsCount
    }
    userErrors {
      field
      message
      code
    }
  }
}

 

 

Variables:

{
  "definition": {
    "name""Ingredients2",
    "namespace""bakery",
    "key""ingredients",
    "description""A list of ingredients used to make the product.",
    "type""multi_line_text_field",
    "ownerType""PRODUCT"
  }
}
Liam
Shopify Staff
2873 312 820

Great to hear this is working as expected - for creating the metafield definition on the admin, I mean actually manually creating in on the store admin at Settings > Custom Data.

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

AmoghRaghuram93
Shopify Partner
2 0 0

Hi Liam,

 

Can this be done using REST APIs instead of GraphQL?