Offers for an Excel of Parts
Understand how to use Python and the Sourcengine API to get offers for a list of MPN and Manufacturer combo in an Excel
The Sourcengine API is a powerful tool that offers extensive capabilities for finding detailed information on electronic components (parts) and exploring available offers for those parts. It provides developers with access to a vast database of electronic components and their associated offers from various suppliers, empowering them to build applications that enable efficient electronic component sourcing and procurement. Let's delve into the details of the Sourcengine API's power and features:
Here's a small tutorial on how to create a Python project that parses an Excel file and queries the Sourcengine API for parts and offers. In this example, we'll use the openpyxl
library and the pandas
library to handle Excel files and the requests
library to make API requests. This tutorial assumes you have some familiarity with Python and have already obtained an API key from Sourcengine.
Set Up Your Environment
- Install Dependencies: Open your terminal and install the required libraries using
pip
:
pip install pandas openpyxl requests
- Create a New Directory: Create a new directory for your project and navigate to it in the terminal.
Parse Excel File
Let's assume you have an Excel file named test.xlsx
with a column named "MPN" containing Manufacturer Part Number and a column named "MFR" containing the manufacturer name.
test.xlsx:
MPN | MFR |
---|---|
ABC123 | Texas Instruments |
DEF456 | Texas Instruments |
GHI789 | Texas Instruments |
Query Sourcengine API
Create a Python script named sourcengine_query.py
in your project directory:
import openpyxl
import pandas as pd
import requests
API_TOKEN = 'YOUR_TOKEN'
def match_parts(searches):
headers = {
'accept': 'application/json',
'content-type': 'application/json',
'authorization': 'Bearer ' + API_TOKEN
}
print('Matching requested parts...')
response = requests.post('https://catalog.sourcengine.com/api/parts/search-multiple', headers=headers, json={'searches': searches}).json()
return response['results']
def get_offers(sku):
headers = {
'content-type': 'application/json',
'authorization': 'Bearer ' + API_TOKEN
}
print('Fetching offers for: ' + sku)
return requests.get('https://catalog.sourcengine.com/api/parts/'+ sku +'/offers/search', headers=headers).json()
# Parse Excel and get the list of requested parts
# Just a normal Excel file with 2 columns: MPN and MFR
parts_to_search = []
input_sheet = pd.read_excel('test.xlsx')
for i, row in input_sheet.iterrows():
# From the Excel, get the MPN and Manufacturer and send to the API
parts_to_search.append({
'mpn': row['MPN'],
'manufacturer': row['MFR'],
})
# Call the Sourcengine API with the requested parts in 2 steps:
#
# First we make a call to match the MPN and Manufacturer to a SKU
# Then, from the SKU, we get offers
results = match_parts(parts_to_search)
lines = []
for result in results:
for part in result['parts']:
# Get the part information
line = {
'MPN': part['mpn'],
'Manufacturer': part['manufacturer'],
'Date Code': '',
'Packaging Type': '',
'MOQ': '',
'MPQ': '',
'Available Quantity': '',
'Delivery Days': '',
'Price': '',
}
# Now that we have a match, get offers for that SKU
offers = get_offers(part['sku'])
for offer in offers['results']:
for tier in offer['priceTiers']:
price = float(tier['price'])
line = {**line, **{
'Date Code': offer['dateCode'],
'Packaging Type': offer['packagingType'],
'MOQ': tier['moq'],
'MPQ': offer['mpq'],
'Available Quantity': offer['quantity'],
'Delivery Days': offer['deliveryDays'],
'Price': price,
}}
lines.append(line)
# Write output CSV
df = pd.DataFrame(lines)
with pd.ExcelWriter('output.xlsx') as writer:
df.to_excel(writer, index=False)
Replace YOUR_TOKEN
with your actual Sourcengine API token.
Run the Script
In your terminal, navigate to the project directory and run the script:
python sourcengine_query.py
The script will iterate through the Excel file, query the Sourcengine API for each MPN, and save a new Excel file called output.xlsx
that contains all the offers for every part.
Updated over 1 year ago