Can't find customer metafield via API (python)

Topic summary

A developer encountered an issue where custom customer metafields (namespace: custom, key: kundennummer) weren’t appearing when retrieved via the Shopify Python API, despite being populated in customer profiles.

Initial Setup:

  • Using API version 2024-04
  • Had read/write access enabled for metafields
  • Standard shopify.Metafield.find() method only returned metafields from other sources (e.g., cookie banner app), not custom customer fields

Solution Found:
The developer resolved the issue by:

  1. Using a direct REST API endpoint instead of the Python library’s built-in method:
    /admin/api/{version}/customers/{customer_id}/metafields.json
    
  2. Increasing the limit parameter to 250 (from default 100), as only the first 100 metafields were being returned initially
  3. Specifying the correct namespace (customer_fields) in the request parameters

The issue was resolved independently, though the exact cause (whether related to API scopes, library limitations, or pagination) wasn’t fully clarified.

Summarized with AI on November 8. AI used: claude-sonnet-4-5-20250929.

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!

Found the solution by myself :slightly_smiling_face:

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

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.