Metafield created with Rest API, not appearing on Backend

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

Products

Any clues?

Hi RfcSilva,

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

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?

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.

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($definition: MetafieldDefinitionInput!) {
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”
}
}

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.

Hi Liam,

Can this be done using REST APIs instead of GraphQL?