Can't find customer metafield via API (python)

Solved

Can't find customer metafield via API (python)

cKog4489
Shopify Partner
3 1 0
 

Hi everyone,

I am new to working with the Shopify API and currently developing a simple app for a client. Part of this app involves updating a custom metafield in the customer profile. However, when I retrieve customer data and print a list of all metafields, the custom metafield (namespace: custom, key: kundennummer) is not listed, even though the customer has filled out these fields.

Here is the relevant part of my code:

 

 

try:
    # FIND CUSTOMER NUMBER
    client_number = None  # Initialize client_number

    metafields = shopify.Metafield.find(owner_id=customer_id, owner_resource="customer")

    if metafields:
        for mf in metafields:
            if mf.namespace == "custom" and mf.key == "kundennummer":
                client_number = mf.value
                print(f"Customer number found: {client_number}")
                break
    else:
        print(f"Customer {customer.id} has no metafields.")
        client_number = "unknown"

except Exception as e:
    # Error handling
    print(f"Error retrieving metafields: {e}")

 

 

 

I am using API version 2024-04, and in the metafield creation, I have enabled both write and read access for API access.

What am I missing or doing wrong?

Please note: when I print a list of all metafields, it shows various metafields from, for example, a cookie banner app, but not the custom metafields I have set up.

Any insights or suggestions would be greatly appreciated!

 
Accepted Solution (1)
cKog4489
Shopify Partner
3 1 0

This is an accepted solution.

I needed to access the API via an extra endpoint 

endpoint = f"https://{SHOP_NAME}.myshopify.com/admin/api/{API_VERSION}/customers/{customer_id}/metafields.json"
    
    params = {
        'metafield[owner_resource]': 'customer',
        'limit': 250,  # Höchstwert pro Seite (maximal 250),
        'metafield[namespace]': 'customer_fields',
    }
    
    try:
        response = requests.get(endpoint, params=params, auth=(API_KEY, PASSWORD))
        response.raise_for_status()  # Überprüft, ob ein HTTP-Fehlerstatus zurückgegeben wurde
        return response.json().get('metafields', [])
    except requests.exceptions.RequestException as e:
        print(f"Fehler beim Abrufen der Metafelder: {e}")
        return None

 

Also I had to increase the limit because only the first 100 metafields were shown, hence I never received the correct metafield data before.

View solution in original post

Replies 3 (3)

cKog4489
Shopify Partner
3 1 0

Found the solution by myself 🙂 

Liam
Shopify Staff
2863 313 817

Out of interest, what was the solution? Did you need more scopes?

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

cKog4489
Shopify Partner
3 1 0

This is an accepted solution.

I needed to access the API via an extra endpoint 

endpoint = f"https://{SHOP_NAME}.myshopify.com/admin/api/{API_VERSION}/customers/{customer_id}/metafields.json"
    
    params = {
        'metafield[owner_resource]': 'customer',
        'limit': 250,  # Höchstwert pro Seite (maximal 250),
        'metafield[namespace]': 'customer_fields',
    }
    
    try:
        response = requests.get(endpoint, params=params, auth=(API_KEY, PASSWORD))
        response.raise_for_status()  # Überprüft, ob ein HTTP-Fehlerstatus zurückgegeben wurde
        return response.json().get('metafields', [])
    except requests.exceptions.RequestException as e:
        print(f"Fehler beim Abrufen der Metafelder: {e}")
        return None

 

Also I had to increase the limit because only the first 100 metafields were shown, hence I never received the correct metafield data before.