This is a digikey api v4 script that is focused only on just the flow needed to get product information. (FYI I'm running this script in a ubuntu linux context, so it has not been tested on windows or mac)

Recommend Reading:

Design Principle: This script is written to be self contained besides requests and for each callout function to be composable, bu lacks much of more advance oauth implementation like token caching.

Also this is restricted to sandboxed api at the moment... however this is okay as this was done as a learning exercise in addition to being for one-off internal projects.

Other Similar Code:

Anyway uploaded this script to gist https://gist.github.com/mofosyne/e15bb122024ff97ecc707dac68ab92b5 and to this page so people can use this if needed to solve their problems. If you do, then let me know that it helped you!

Also by the way, I attempted to exclude the authorisation token because the https://developer.digikey.com/products/product-information-v4/productsearch/keywordsearch?prod=false page for the keyword search didn't have a red required flag next to it... but it turns out you still do need it in addition to the X-DIGIKEY-Client-Id which is extremely annoying... but it is what it is. Its working at least for me. Just don't forget to load in your client ID and secret to the bash enviroment.

#!/usr/bin/env python3

# digikey_api_v4_product_search.py
# Digikey Basic Product Search Script (Targeting Digikey API v4)
# By Brian Khuu 2024

# This is a digikey api v4 script that is focused only on just the flow needed to get product information.
# Recommend Reading:
#   - https://developer.digikey.com/documentation
#       - Section "OAuth 20: 2 Legged Flow" has good information on basic authnetic

# Design Principle: This script is written to be self contained besides requests and for each callout function to be composable
#                   This lacks much of more advance oauth implementation like token caching.
#                   Also this is restricted to sandboxed api at the moment...
#                   However this is okay as this was done as a learning exercise in addition to being for one-off internal projects.

# Other Similar Code:
#    - https://github.com/alvarop/dkbc/blob/master/dkbc/dkbc.py
#         - Just found this while coding this up which was very helpful in figuring out what I was missing

import os
import json
import requests

DIGIKEY_CLIENT_ID=os.getenv("DIGIKEY_CLIENT_ID")
DIGIKEY_CLIENT_SECRET=os.getenv("DIGIKEY_CLIENT_SECRET")

DIGIKEY_AUTH_URL_V4  = 'https://sandbox-api.digikey.com/v1/oauth2/authorize'
DIGIKEY_TOKEN_URL_V4 = 'https://sandbox-api.digikey.com/v1/oauth2/token'
DIGIKEY_PRODUCT_SEARCH_URL_V4 = 'https://sandbox-api.digikey.com/products/v4/search/keyword'

# Sanity Check That Authorisation Details Was Provided
if not DIGIKEY_CLIENT_ID or not DIGIKEY_CLIENT_SECRET:
    print("Missing client id or secret")
    quit()

def oauthV2_get_simple_access_token(url, client_id, client_secret):
    # Get the simple access token required for 2 Legged Authorization OAutV2.0 flow
    # This is typically used for basic search and retreival of publically avaliable information
    response = requests.post(
        url,
        data={
            "client_id": client_id,
            "client_secret": client_secret,
            "grant_type": "client_credentials",
            }
    )
    return response.json()

def oauthv2_product_search(url, client_id, token, keyword):
    # Dev Note: Why did I not just place the data?
    # https://stackoverflow.com/questions/15737434/python-requests-module-urlencoding-json-data
    data_payload = {
        "Keywords": str(keyword),
    }
    response = requests.post(
        url,
        headers = {            
            "X-DIGIKEY-Client-Id": client_id,
            "authorization": "Bearer {access_token}".format(access_token=token.get("access_token")),
            "content-type": "application/json",
            "accept": "application/json",
            },
        data = json.dumps(data_payload)
    )
    return response.json()

oauth_token = oauthV2_get_simple_access_token(DIGIKEY_TOKEN_URL_V4, DIGIKEY_CLIENT_ID, DIGIKEY_CLIENT_SECRET)
print(oauth_token)

search_result = oauthv2_product_search(DIGIKEY_PRODUCT_SEARCH_URL_V4, DIGIKEY_CLIENT_ID, oauth_token, "MCP2221A-I/SL-ND")

print(json.dumps(search_result, indent=4))