Seeking Assistance with 404 Error When Fetching Data from CrUX API

101 views
Skip to first unread message

Bunya

unread,
Nov 11, 2023, 11:38:09 PM11/11/23
to Chrome UX Report (Discussions)
Hello,

I am attempting to fetch Core Web Vitals data for specific domains using the Google Chrome User Experience Report (CrUX) API, but I am encountering the following error:

Error fetching data for [domain name]: 404

This error occurs despite using a newly generated and active API key. I've tried this with several domains confirmed to be present in the CrUX dataset, but all attempts result in the same 404 error.

The environment I am running this in is Google Cloud Platform's Compute Engine. I am using Python and the requests library. I also tried different patterns of query parameters, including those without specifying metrics, but to no avail.

Here is the main part of my code:
*domain is example but I got same result.

import requests
API_KEY = 'DUMMY_ DUMMY  _ DUMMY'
CRUX_API_ENDPOINT = 'https://chromeuxreport.googleapis.com/v1/records:queryRecord'
domain = "www.amazon.com"

def get_core_web_vitals(domain: str) -> dict:
    params = {
        'key': API_KEY,
        'url': f'https://{domain}',
        'metrics': ['largest_contentful_paint', 'first_input_delay', 'cumulative_layout_shift']
    }
    response = requests.get(CRUX_API_ENDPOINT, params=params)
    if response.status_code == 200:
        return response.json()
    else:
        print(f'Error fetching data for {domain}: {response.status_code}')
        return {}

data = get_core_web_vitals(domain)

I would greatly appreciate any advice on this issue. 

Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Barry Pollard

unread,
Nov 13, 2023, 6:05:22 AM11/13/23
to Bunya, Chrome UX Report (Discussions)
The CrUX API endpoint takes a POST request rather than a GET request.

Just changing the response = requests.get to response = requests.post makes your code work.
Though technically, the docs say the key should be sent in the URL and the rest of the params in the body, but seems to work putting everything in the URL (or everything in the body).

Barry


--
You received this message because you are subscribed to the Google Groups "Chrome UX Report (Discussions)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chrome-ux-repo...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chrome-ux-report/5af9cacc-448d-4dd8-8f98-947da9062cc8n%40chromium.org.

Bunya

unread,
Nov 13, 2023, 10:26:37 AM11/13/23
to Chrome UX Report (Discussions), barryp...@google.com, Chrome UX Report (Discussions), Bunya
Thank you Barry!
2023年11月13日月曜日 20:05:22 UTC+9 barryp...@google.com:
Reply all
Reply to author
Forward
0 new messages