How to use SQLAlchemy ORM to access Sage X3 Cloud Data in Python

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Create Python applications and scripts that use SQLAlchemy Object-Relational Mappings of Sage X3 Cloud data.

The rich ecosystem of Python modules lets you get to work quickly and integrate your systems effectively. With the CData Python Connector for Sage X3 Cloud and the SQLAlchemy toolkit, you can build Sage X3 Cloud-connected Python applications and scripts. This article shows how to use SQLAlchemy to connect to Sage X3 Cloud data to query Sage X3 Cloud data.

With built-in optimized data processing, the CData Python Connector offers unmatched performance for interacting with live Sage X3 Cloud data in Python. When you issue complex SQL queries from Sage X3 Cloud, the CData Connector pushes supported SQL operations, like filters and aggregations, directly to Sage X3 Cloud and utilizes the embedded SQL engine to process unsupported operations client-side (often SQL functions and JOIN operations).

Connecting to Sage X3 Cloud Data

Connecting to Sage X3 Cloud data looks just like connecting to any relational data source. Create a connection string using the required connection properties. For this article, you will pass the connection string as a parameter to the create_engine function.

Sage X3 Cloud uses the OAuth 2.0 Client Credentials flow, and an X-API-Key is also required for API access. Set AuthScheme to OAuth and specify the following connection properties:

  • URL: The base URL of your Sage X3 Cloud instance.
  • OAuthAccessTokenUrl: The OAuth token endpoint (e.g., https://your-auth-domain/oauth/token).
  • OAuthClientId: Your OAuth application client ID.
  • OAuthClientSecret: Your OAuth application client secret.
  • Audience: The API audience value for the token request.
  • XAPIKey: The X-API-Key provided by your Sage X3 Cloud administrator.
  • Folder: The Sage X3 folder name (e.g., SEED). This folder is used as the default schema.
  • Folders (optional): A comma-separated list of Sage X3 folders (e.g., SEED,PERF). Each folder is exposed as a separate schema, so you can query across folders with the Schema.Table syntax.

The driver obtains an access token with the Client Credentials flow and sends it with the X-API-Key on every API request. With InitiateOAuth set to GETANDREFRESH (the default), the driver acquires and refreshes the token automatically.

Follow the procedure below to install SQLAlchemy and start accessing Sage X3 Cloud through Python objects.

Install Required Modules

Use the pip utility to install the SQLAlchemy toolkit and SQLAlchemy ORM package:

pip install sqlalchemy
pip install sqlalchemy.orm

Be sure to import the appropriate modules:

from sqlalchemy import create_engine, String, Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Model Sage X3 Cloud Data in Python

You can now connect with a connection string. Use the create_engine function to create an Engine for working with Sage X3 Cloud data.

NOTE: Users should URL encode the any connection string properties that include special characters. For more information, refer to the SQL Alchemy documentation.


engine = create_engine("sagex3cloud:///?AuthScheme=OAuth&URL=https://x3server/&OAuthAccessTokenUrl=https://auth-domain/oauth/token&OAuthClientId=your_client_id&OAuthClientSecret=your_client_secret&Audience=https://api-audience&XAPIKey=your_api_key&Folder=SEED")

Declare a Mapping Class for Sage X3 Cloud Data

After establishing the connection, declare a mapping class for the table you wish to model in the ORM (in this article, we will model the BPCUSTOMER table). Use the sqlalchemy.ext.declarative.declarative_base function and create a new class with some or all of the fields (columns) defined.


base = declarative_base()
class BPCUSTOMER(base):
	__tablename__ = "BPCUSTOMER"
	BPCNUM = Column(String,primary_key=True)
	BPCNAM = Column(String)
	...

Query Sage X3 Cloud Data

With the mapping class prepared, you can use a session object to query the data source. After binding the Engine to the session, provide the mapping class to the session query method.

Using the query Method


engine = create_engine("sagex3cloud:///?AuthScheme=OAuth&URL=https://x3server/&OAuthAccessTokenUrl=https://auth-domain/oauth/token&OAuthClientId=your_client_id&OAuthClientSecret=your_client_secret&Audience=https://api-audience&XAPIKey=your_api_key&Folder=SEED")
factory = sessionmaker(bind=engine)
session = factory()
for instance in session.query(BPCUSTOMER).filter_by(BPCNUM="MARTIN"):
	print("BPCNUM: ", instance.BPCNUM)
	print("BPCNAM: ", instance.BPCNAM)
	print("---------")

Alternatively, you can use the execute method with the appropriate table object. The code below works with an active session.

Using the execute Method


BPCUSTOMER_table = BPCUSTOMER.metadata.tables["BPCUSTOMER"]
for instance in session.execute(BPCUSTOMER_table.select().where(BPCUSTOMER_table.c.BPCNUM == "MARTIN")):
	print("BPCNUM: ", instance.BPCNUM)
	print("BPCNAM: ", instance.BPCNAM)
	print("---------")

For examples of more complex querying, including JOINs, aggregations, limits, and more, refer to the Help documentation for the extension.

Free Trial & More Information

Download a free, 30-day trial of the CData Python Connector for Sage X3 Cloud to start building Python apps and scripts with connectivity to Sage X3 Cloud data. Reach out to our Support Team if you have any questions.

Ready to get started?

Download a free trial of the Sage X3 Cloud Connector to get started:

 Download Now

Learn more:

Sage X3 Cloud Icon Sage X3 Cloud Python Connector

Python Connector Libraries for Sage X3 Cloud Data Connectivity. Integrate Sage X3 Cloud with popular Python tools like Pandas, SQLAlchemy, Dash & petl.