How to create a definition for a metafield in API?

Djurmamman
Explorer
63 4 11

I'm POSTing a metafield to order thru the rest API. How do create a definition for the metafield?

Replies 3 (3)

ColesBalloons
Excursionist
18 1 3
Metafields *enable you to customize the functionality and appearance of
your Shopify store by letting you save specialized information that isn't
usually captured in the Shopify admin*. You can use metafields for internal
tracking, or to display specialized information on your online store in a
variety of ways.
Djurmamman
Explorer
63 4 11

I know. 

 

My question was how to define definition via the rest API.

NunoIsm
Shopify Partner
1 0 0

Unfortunately, that isn't possible in REST API for now as far as i know. 

I encountered the same problem and it is possible using the GraphQL API . For my project i use PHPShopify SDK and as you can see in the documentation, it is possible to use GraphQL using that.  

I know this post is kind of old, but this might help future people.

 

Here is an example of what i used.

$input = [
            "definition" => [
                "namespace" => 'my_namespace',
                "key" => 'my_key',
                "type" => 'my_type,
                "name" => 'my_name',
                "ownerType" => "PRODUCT",
                "visibleToStorefrontApi" => true,
                "description" => 'this is a description'
            ]
        ];

        $graphQL = <<<Query
        mutation MetafieldDefinitionCreate(\$definition: MetafieldDefinitionInput!) {
          metafieldDefinitionCreate(definition: \$definition) {
            createdDefinition {
              id
              namespace
              key
              type {
                name
              }
              name
              ownerType
              visibleToStorefrontApi
              description
            }
            userErrors {
              field
              message
            }
          }
        }
        Query;

        $responseCreate = $shopify->GraphQL->post($graphQL, null, null, $input);