Finding Offers for a Part

Understand how to use the Sourcengine API with Python to find offers for a given part

Set Up Your Environment

Before we begin, make sure you have Python installed on your system. You can download it from the official website: https://www.python.org/downloads/

We'll also be using the requests library to make API requests. If you don't have it installed, you can do so using pip:

pip install requests

Obtain API Key

To access the Sourcengine API, you need an API key. If you don't have one, please reach out to your account manager.

Understanding the API

In the context of the Sourcengine API, there are two primary types of searches: searching for parts and searching for offers. Let's break down the difference between these two types of searches:

Searching for Parts

When you search for parts using the Sourcengine API, you are querying the database for electronic components based on various parameters such as Manufacturer Part Number (MPN), Manufacturer, category, full text search, and more. This type of search helps you find detailed information about specific electronic components, including their specifications, datasheets, availability, and potentially multiple offers from different suppliers.

For example, if you're looking for a specific type of microcontroller, you can use the API to search for it by providing its MPN or other relevant details. The API will return information about the microcontroller itself, such as its technical specifications, package type, pin configuration, and more. This type of search is useful when you want to gather detailed information about electronic components to make informed purchasing decisions.

Searching for Offers

On the other hand, when you search for offers using the Sourcengine API, you are looking for available deals or offers for a particular electronic component, typically identified by its SKU. Offers represent the available inventory from various suppliers along with details such as pricing, stock quantity, delivery time, and supplier information.

For instance, if you have a specific SKU in mind and you want to know where you can buy that component, at what price, and how much stock is available, you would perform a search for offers. The API will return a list of offers from different suppliers that have the requested component in stock, along with their pricing and other relevant details.

In summary, the main difference between searching for parts and searching for offers in the Sourcengine API is:

  • Searching for Parts: Involves querying the database to retrieve detailed information about electronic components based on various parameters, helping you understand the technical specifications and characteristics of the components.

  • Searching for Offers: Involves querying the database to find available deals or offers for a specific electronic component, showing you where and at what price you can purchase the component from different suppliers.

Both types of searches serve different purposes and can be used in various scenarios, depending on whether you're interested in exploring component details or finding the best purchasing options for your specific needs.

Make API Requests

Now let's write a Python script to search for offers based on an MPN and Manufacturer using the Sourcengine API:

import requests

API_TOKEN = 'YOUR_TOKEN'

def match_parts(searches):
    headers = {
        'accept': 'application/json',
        'content-type': 'application/json',
        'authorization': 'Bearer ' + API_TOKEN
    }

    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()


results = match_parts([{'mpn': 'MAX232IN', 'manufacturer': 'Texas Instruments'}])
for result in results:
    for part in result['parts']:
        print('Matched: ' + part['sku'])
        print('MPN Match type: ' + part['match']['mpn'])
        print('Manufacturer match: ' + str(part['match']['manufacturerMatched']))

        # Now that we have a match, search for offers
        offers = get_offers(part['sku'])
        for offer in offers['results']:
            for tier in offer['priceTiers']:
                price = float(tier['price'])
                details = {
                    'Date Code': offer['dateCode'],
                    'Packaging Type': offer['packagingType'],
                    'MOQ': tier['moq'],
                    'MPQ': offer['mpq'],
                    'Available Quantity': offer['quantity'],
                    'Delivery Days': offer['deliveryDays'],
                    'Price': price,
                }

                print(details)

Replace YOUR_TOKEN, with your actual API key.

Step 4: Run the Script

Save the script to a .py file and run it using your terminal:

python search_offers.py