Search

Getting Started with Python

Getting Started with Python

Lets start by walking through how to setup an environment and install the Kodexa Python SDK. This SDK can be used to interact with almost all aspects of Kodexa’s Documents and Platfrom.

Installing Python 3.11

  1. Visit the official Python website: https://www.python.org/downloads/
  2. Download the Python 3.11 installer for your operating system
  3. Run the installer and follow the installation wizard
  4. Make sure to check the box that says "Add Python 3.11 to PATH" during installation

Setting up a Virtual Environment

  1. Open a terminal or command prompt
  2. Navigate to your project directory
  3. Create a new virtual environment:
  4. python -m venv kodexa_env
  5. Activate the virtual environment:
    • On Windows:
    • kodexa_env\\Scripts\\activate
    • On macOS and Linux:
    • source kodexa_env/bin/activate

Installing Kodexa

With your virtual environment activated, install Kodexa using pip:

pip install kodexa

Connecting to Kodexa Using a Python SDK

  1. Go to the Kodexa Platform: kodexa.platform.ai
  2. Click on your profile picture in the top right corner and select Profile
  3. Navigate to the tab listed API Tokens and then click on New Access Token
  4. Enter the name of your token and click Generate New Access Token to create and view the token
  5. Copy the token to your clipboard as you will not be able to view it again after creation

Writing a Script to Connect to KodexaClient

Create a new Python file called kodexa_script.py and add the following code:

import os
from kodexa.client.client import KodexaClient

# Set up environment variables
os.environ['KODEXA_URL'] = '<https://your-kodexa-url.com>'
os.environ['KODEXA_ACCESS_TOKEN'] = 'your-access-token'

# Create a KodexaClient instance
client = KodexaClient(
    url=os.environ.get('KODEXA_URL'),
    access_token=os.environ.get('KODEXA_ACCESS_TOKEN')
)

# Use the client to interact with the Kodexa platform
# For example, get information about the current user
me = client.me
print(f"Logged in as: {me.email}")

# Add more Kodexa operations here

This script demonstrates how to use environment variables to securely store your Kodexa URL and access token. It then creates a KodexaClient instance using these environment variables and performs a simple operation to get information about the current user.

To run the script, make sure your virtual environment is activated and then execute:

python kodexa_script.py

Remember to replace '<https://your-kodexa-url.com>' and 'your-access-token' with your actual Kodexa URL and access token.

By using environment variables, you can keep sensitive information out of your code and make it easier to manage different configurations for development, testing, and production environments.