Example Implementations

When interacting with Datadome a good TLS Client is required

Note that the interstitial challenge will return a json, with other than the cookie, an url to follow. In case of hard-protection mode on datadome, you may get redirected to a captcha challenge that needs to be solved as well

from tls_client import Session
from requests import post

if __name__ == "__main__":
    API_KEY = "TAKION_API_XXXX" 

    session = Session("chrome_120") 
    page_url = 'https://www.footlocker.pt/en/product/~/314206535404.html'
    page_headers = {
        'Host': 'www.footlocker.pt',
        'cache-control': 'max-age=0',
        'sec-ch-device-memory': '8',
        'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-arch': '"arm"',
        'sec-ch-ua-platform': '"macOS"',
        'sec-ch-ua-model': '""',
        'sec-ch-ua-full-version-list': '"Not/A)Brand";v="8.0.0.0", "Chromium";v="126.0.6478.127", "Google Chrome";v="126.0.6478.127"',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'sec-fetch-site': 'none',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-user': '?1',
        'sec-fetch-dest': 'document',
        'accept-language': 'en-GB,en;q=0.9',
        'priority': 'u=0, i',
    }

    # 1. Load the page
    response = session.get(page_url, headers=page_headers)

    # 2. Check for Datadome
    if not (("geo.captcha-delivery.com" in response.text or \
            "interstitial.captcha-delivery.com" in response.text) \
                and response.status_code == 403): # Check if we need to solve a challenge
        print("Page loaded successfully")
        exit(0)
    
    # 3. Generate challenge url
    response = post(
        "https://datadome.takionapi.tech/build",
        headers={
            "x-api-key": API_KEY,
        },
        json={
            "datadome": session.cookies.get_dict().get("datadome"),
            "referer": page_url,
            "html": response.text
        }
    ).json()

    if response.get("error"):
        print("Task failed with error => %s" % response["error"])
        exit(0)

    challenge_url = response["url"]
    
    # 4. Solve the challenge
    print(f"Solving {response['challenge_type']} challenge...")
    challenge = session.get(
        challenge_url, 
        headers={
            'Host': 'geo.captcha-delivery.com',
            'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"macOS"',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'Sec-Fetch-Site': 'cross-site',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Dest': 'iframe',
            'Referer': 'https://www.footlocker.pt/en/product/~/314206535404.html',
            'Accept-Language': 'en-GB,en;q=0.9',
        }
    )
    data = post(
        f"https://datadome.takionapi.tech/solve",
        headers={
            "x-api-key": API_KEY,
            "User-Agent": 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
        },
        json={
            "html": challenge.text
        }
    ).json()
    if error := data.get("error"):
        print("Task failed with error => %s" % error)
        exit(0)

    # 5. Post the solution to Datadome
    print("Posting solution...")
    
    # For interstitial is a POST request, footlocker only uses it
    res = session.post(
        data["url"],
        data=data["payload"],
        headers={
            # Headers took from our browser
            'Host': 'geo.captcha-delivery.com',
            'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
            'sec-ch-ua-platform': '"macOS"',
            'sec-ch-ua-mobile': '?0',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Accept': '*/*',
            'Sec-Fetch-Site': 'same-origin',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Dest': 'empty',
            'Referer': 'https://geo.captcha-delivery.com/captcha/?initialCid=AHrlqAAAAAMAyaXgoxstywMAs2mAaw%3D%3D&hash=EC3B9FB6F2A31D3AF16C270E6531D2&cid=WnaMs0Z7esaErwyCZ5bgStxks4ZNPTM43srPi1nPou7gj6cYdKWPr3WMHkqqO1GS3v8Dnbg5JN4O1l4lMScX9TesXJvvy67uqtRjDIzLM8dap3xjH~zSxlrIZeFcyzaG&t=fe&referer=https%3A%2F%2Feuro2024-sales.tickets.uefa.com%2F&s=43337&e=511a3fab6473017b5c67ee503b9dc67701795c127fc3898022ea13fb22e74965&dm=cd',
            'Accept-Language': 'en-GB,en;q=0.9',
        }
    )
    data = res.json()
    if cookie := data["cookie"].split("datadome=")[1].split("; ")[0]:
        print(f"Got datadome cookie => {cookie[:30]}")
        session.cookies.set("datadome", cookie)
    
    # 6. Load the page again
    response = session.get(page_url, headers=page_headers)
    print(response)

Last updated