
Use Digikey API with Python

Here are the steps to implement the Digikey API into your Python Script.
- In a new folder make a new Python file called “DigikeyAPI.py”, open and navigate to that folder in a new terminal
- Pip install hurricaneJoef’s branch of Digikey-API (as the main branch hasn’t been updated for ProductInformation V4)
pip install git+https://github.com/hurricaneJoef/digikey-api.git
- Paste this code into the terminal to set up the storage folder for the API keys
export DIGIKEY_CLIENT_ID="client_id" export DIGIKEY_CLIENT_SECRET="client_secret" export DIGIKEY_STORAGE_PATH="cache_dir"
- Go to https://developer.digikey.com/ login and click organizations at the top of the webpage
- Click Create Organization > give your organization a name > Click Add Organization
- Click the Production Apps tab > Click create Production App Button
- Add a name to the Production App name field
- Add https://localhost:8139/digikey_callback to the OAuth Callback Field
- Select the Product Information V4 check box and click save
- Click the View tab. In Credentials click show key and copy the Client ID and Client Secret
- Go back to DigikeyAPI.py and paste the code in below
''' As default version of digikey-api is broken Use: pip install git+https://github.com/hurricaneJoef/digikey-api.git Run: export DIGIKEY_CLIENT_ID="client_id" export DIGIKEY_CLIENT_SECRET="client_secret" export DIGIKEY_STORAGE_PATH="cache_dir" ''' import os from pathlib import Path import digikey from digikey.v4.productinformation import KeywordRequest CACHE_DIR = str(Path("$cache_dir")) #Windows usage os.environ["DIGIKEY_STORAGE_PATH"] = CACHE_DIR os.environ['DIGIKEY_CLIENT_ID'] = 'your client ID' os.environ['DIGIKEY_CLIENT_SECRET'] = 'your client secret' os.environ['DIGIKEY_CLIENT_SANDBOX'] = 'False' part = 'INA260AIPWR' # Edit to search for a different part # Search for parts search_request = KeywordRequest(keywords= part, limit=10, offset = 0) result = digikey.keyword_search(body=search_request) # print(result) # Look at all Digikey data for product in result.products: mpn = getattr(product, "manufacturer_product_number", "Not found") catg = getattr(product.category, "name", "Not Found" ) detailed_desc = getattr(product.description, "detailed_description", "Not found") print("\n") print(f"Category: {catg}") print(f"Part Number: {mpn}") print(f"Detailed Description: {detailed_desc}\n")
- Add your Client ID and Secret key to these lines and run the code
os.environ['DIGIKEY_CLIENT_ID'] = 'your client ID' os.environ['DIGIKEY_CLIENT_SECRET'] = 'your client secret'
- The code will now create the $cache folder and open a browser retrieving the Authcode for the app
- The terminal will now print out the detailed description, part number and category values for the part
Category: Integrated Circuits (ICs) Part Number: INA260AIPWR Detailed Description: Current Monitor Regulator High/Low-Side
- Change the part variable in the code to search for a new part
- The Read Me code in hurricaneJoef’s branch of Digikey-API currently has errors. Use KeywordRequest instead of KeywordSearchRequest (see pull request)
- You can output all of the values for the part by uncommenting this line
# print(result) # Look at all Digikey data
Buy Me a Coffee
Coffee is getting expensive these days! Buy me a coffee and help keep the projects flowing. Click to donate