B2B API companyAssignCustomerAsContact issue

Topic summary

Issue Identified:
User encountered an “invalid id” error when attempting to use the companyAssignCustomerAsContact GraphQL mutation to link a customer as a contact to a company in Shopify’s B2B API.

Root Cause:
The mutation arguments were incorrectly using gid://shopify/Product/ instead of the proper resource types:

  • customerId should use gid://shopify/Customer/[id]
  • companyId should use gid://shopify/Company/[id]

Resolution:
Shopify support identified the error in both the user’s code and the official documentation (which also listed “Product” as placeholder variables). The user successfully resolved the issue by:

  • Correcting the Global ID (GID) format to reference proper resource types
  • Implementing the mutation with proper variables in Python

Additional Notes:

  • Shopify support confirmed they’ve submitted feedback to update the documentation
  • The working solution uses parameterized GraphQL variables rather than string replacement
  • Issue is now resolved
Summarized with AI on November 11. AI used: claude-sonnet-4-5-20250929.

I’m having trouble executing this API GraphQL mutation: companyAssignCustomerAsContact

I have a company and customer created but I cannot create/assign that customer as a contact to that company I was following the documentation and tried implementing it with Python.

import json
import requests

def assign_customer_as_contact(url, access_token, companyId, customerId):
    # Define the GraphQL mutation
    mutation = """
mutation {
  companyAssignCustomerAsContact(
    customerId: "gid://shopify/Product/your_contact_id"
    companyId: "gid://shopify/Product/your_company_id"
  ) {
    userErrors {
      field
      message
    }
  }
}
    """

    # Prepare the variables for the mutation
    mutation = mutation.replace('your_contact_id', customerId)
    mutation = mutation.replace('your_company_id', companyId)

    print(mutation)
    # Setup headers for the request
    headers = {
        "Content-Type": "application/json",
        "X-Shopify-Access-Token": access_token,
    }

    # Setup the request payload
    payload = {"query": mutation}

    # Send the request to the Shopify GraphQL API
    response = requests.post(url, headers=headers, json=payload)

    # Handle the response
    if response.status_code == 200:
        data = response.json()
        if "errors" in data:
            print("Error:", data["errors"])
        else:
            print("Customer assigned as contact successfully.")
            print(json.dumps(data["data"], indent=2))
    else:
        print("Failed to assign customer as contact.", response.text)

if __name__ == "__main__":
    api_url = ''
    api_access_token = ''

    company_id = "3131"
    customer_id = "3131"

    # Call the function to assign customer as contact
    assign_customer_as_contact(api_url, api_access_token, company_id, customer_id)

I got the following error:

Error: [{‘message’: ‘invalid id’, ‘locations’: [{‘line’: 3, ‘column’: 3}], ‘path’: [‘companyAssignCustomerAsContact’]}]

What am I doing wrong I assume they are referring to these customers.

Hey @BojanAnchev ,

I see your script is replacing the legacy resource ID, the number at the end of the Shopify Global ID value, but both arguments to the mutation are set as Products rather than a Customer and a Company:

companyAssignCustomerAsContact(
    customerId: "gid://shopify/Product/your_contact_id"
    companyId: "gid://shopify/Product/your_company_id"
  )

Looks like our documentation for that mutation has Products listed as placeholder variables as well, so I’ve already sent some feedback to request the docs be updated.

All the best!

@ShopifyDevSup thank you, this resolved the issue.

def assign_company_contact(shopify_store, company_id, customer_id):
    graphql_url = get_graphql_endpoint(shopify_store)
    access_token = shopify_store.shopify_api_password
    variables = {
        "companyId": "gid://shopify/Company/%s" % company_id,
        "customerId": "gid://shopify/Customer/%s" % customer_id
    }
    graphql_mutation = """
    mutation companyAssignCustomerAsContact($companyId: ID!, $customerId: ID!) {
      companyAssignCustomerAsContact(companyId: $companyId, customerId: $customerId) {
        companyContact {
          id
          # CompanyContact fields
        }
        userErrors {
          field
          message
        }
      }
    }
    """
    headers = {
        "Content-Type": "application/json",
        "X-Shopify-Access-Token": access_token,
    }

    try:
        response = requests.post(
            graphql_url,
            json={"query": graphql_mutation, "variables": variables},
            headers=headers
        )
        response = response.json()
        logging.info('-- ASSIGN COMPANY CUSTOMER --')
        logging.info(response)
    except Exception as e:
        logging.info("Error linking customer and company:")
        logging.info(e)