Querying library components to a CSV file

Modified on Thu, 19 Oct 2023 at 05:09 PM

This guide is for getting library component data automatically from the Nexar API and outputting it to a CSV file. This process will be done in three steps:

  • Creating an access token, 
  • Using the access token to query the design API, 
  • Writing the query results to a CSV file. 

This guide assumes you have read the Accessing Altium 365 design data guide.


Step 1: Creating an access token


import requests 
 
client_id = "Your Client ID" 
client_secret = "Your Client Secret" 
username = "Your username" 
password = "Your password" 
 
TOKEN_URL = "https://identity.nexar.com/connect/token" 
token_response = requests.post( 
        url=TOKEN_URL, 
        data={ 
            "grant_type": "password", 
            "client_id": client_id, 
            "client_secret": client_secret, 
            "username": username, 
            "password": password, 
        }, 
    ).json() 
 
token = token_response["access_token"] 


This code shows how to get an access token from the Nexar API. It will send a request to the URL provided with the dictionary "data". This will return a dictionary of data including the access token so line 20 sets the token to just the access token. The client_id and client_secret are available in portal.nexar.com, in the application you want to use. The username and password are the ones used for portal.nexar.com or Altium 365. After the access token is generated, it is valid for 24 hours so can be reused.


Step 2: Querying the design API


LIBRARY_COMPONENTS_QUERY = '''
query GetLibrary($workspaceUrl : String!) {
  desLibrary(
    workspaceUrl: $workspaceUrl
  ) {
    components{
      nodes{
        id  
        name
        description
        comment
      }
    }
  }
}
'''

workspace_url = "Your workspace URL"

variables = {
    "workspaceUrl": workspace_url
}

library_components = requests.post("https://api.nexar.com/graphql", json={"query": LIBRARY_COMPONENTS_QUERY, "variables": variables}, headers={"Authorization": f"Bearer {token}"}).json()["data"]["desLibrary"]["components"]["nodes"]


This code shows how to query the design API. First it will set the variables needed for the query, in this case, the URL it will get the component information from. On line 24, it will make the request. The requests module is used to make the request, and it uses requests.post to send an HTTP POST request. It will send the request to the URL inside of requests.post with the query LIBRARY_COMPONENTS_QUERY which is defined on line 1, and the Authorization header which has the token generated in step 1. 


Step 3: Writing query result to a CSV file


import csv

with open('library_components.csv', 'w', newline="") as components_csv:

  writer = csv.DictWriter(components_csv, fieldnames=[
      "id",
      "name",
      "description",
      "comment"],extrasaction="ignore")

  writer.writeheader()
  writer.writerows(library_components)


This code shows how to write to a CSV file with the library components returned from a query. It will create a file called 'library_components.csv' and write the components to it. If this file already exists, it will be overwritten when the script executes. For each component, the properties defined in the fieldnames list will be written to the file



Next Steps:


Check out a simple console app which looks up projects in an Altium 365 workspace on Github here.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article