How to get shop name from webhook response

sandeepks23
Shopify Partner
41 0 5

I am unable to get the shop name from the reponse of the location/creation webhook.

 

class LocationCreatedReceiver(APIView):
    def post(self, request, *args, **kwargs):
        """
        This API recieves response from customers/redact Webhook
        """
        print("SEBD")
        try:
            webhook_topic = request.META['HTTP_X_SHOPIFY_TOPIC']
            webhook_hmac = request.META['HTTP_X_SHOPIFY_HMAC_SHA256']
            webhook_data = json.loads(request.body)
        except:
            return HttpResponse('Unauthorized', status=401)
        verified = verify_webhook(request.body, 'webhook_key',
                                  webhook_hmac)
        if not verified:
            return HttpResponse('Unauthorized', status=401)
        # shop_name = extract_shop_name(webhook_data['admin_graphql_api_id'])
        return HttpResponse(status=200)

 and this is the response received when I print webhook_data.

{'id': 68011065541, 'name': 'trivandrum', 'address1': 'ww', 'address2': 'ww', 'city': 'ww', 'zip': '66444444', 'province': 'Andaman and Nicobar Islands', 'country': 'IN', 'phone': '+919494949494', 'created_at': '2023-05-30T13:46:11+05:30', 'updated_at': '2023-05-30T13:46:12+05:30', 'country_code': 'IN', 'country_name': 'India', 'province_code': 'AN', 'legacy': False, 'active': True, 'admin_graphql_api_id': 'gid://shopify/Location/68011065541'}

as you can see there is no information regarding the shop name

Replies 2 (2)

magecomp
Shopify Partner
264 20 32

Hello @sandeepks23 

 

Try this

 
import requests

def get_shop_name(admin_graphql_api_id):
    """
    Gets the shop name from the Shopify API.

    Args:
        admin_graphql_api_id: The admin_graphql_api_id of the shop.

    Returns:
        The shop name.
    """

    url = 'https://api.shopify.com/admin/graphql'
    headers = {
        'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
        'Content-Type': 'application/json',
    }

    body = {
        'query': """
            {
                shop {
                    name
                }
            }
        """,
        'variables': {
            'adminGraphqlApiId': admin_graphql_api_id,
        }
    }

    response = requests.post(url, headers=headers, data=json.dumps(body))

    if response.status_code == 200:
        data = response.json()
        shop_name = data['data']['shop']['name']
        return shop_name
    else:
        raise Exception('Error getting shop name: {}'.format(response.status_code))
Helping voluntarily. Please like and accept the solution if it helps. Thanks!
Our Bestseller Shopify Apps    |      Mobile App Builder by MageComp    |      Limit Qty Purchase

Need a developer?  Just visit MageComp website
sandeepks23
Shopify Partner
41 0 5

Actually I got it. It was in the META header

HTTP_X_SHOPIFY_SHOP_DOMAIN

. Thank you for your response.