CodeWords Client

0.4.8 · active · verified Thu Apr 16

CodeWords Client (0.4.8) is a Python library providing a client for the CodeWords serverless automation platform. It facilitates interaction with CodeWords workflows, especially for FastAPI applications, allowing programmatic execution and management of AI-powered automations. The library is actively maintained with frequent updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the CodeWords client with an API key (preferably from an environment variable) and execute both synchronous (`run`) and asynchronous (`run_async`) CodeWords workflows. It also shows a basic polling mechanism to retrieve results from asynchronous operations. Replace `your-sync-workflow-id` and `your-async-workflow-id` with your actual workflow IDs.

import os
import asyncio
from codewords_client import AsyncCodewordsClient

async def run_workflow():
    api_key = os.environ.get('CODEWORDS_API_KEY', 'your_codewords_api_key_here')
    if not api_key or api_key == 'your_codewords_api_key_here':
        print("Please set the CODEWORDS_API_KEY environment variable or replace the placeholder.")
        return

    async with AsyncCodewordsClient(api_key=api_key) as client:
        try:
            # Example: Run a synchronous workflow (for tasks under 2 minutes)
            print("Running synchronous workflow...")
            sync_result = await client.run(
                service_id="your-sync-workflow-id",
                data={
                    "input_param1": "value1",
                    "input_param2": "value2"
                }
            )
            print(f"Synchronous workflow result: {sync_result}")

            # Example: Run an asynchronous workflow (for longer tasks)
            print("Running asynchronous workflow...")
            async_request = await client.run_async(
                service_id="your-async-workflow-id",
                data={
                    "large_data": ["item1", "item2", "..."]
                }
            )
            print(f"Asynchronous workflow initiated. Request ID: {async_request.request_id}")

            # Poll for async result (simplified)
            while True:
                status_response = await client.get_result(async_request.request_id)
                if status_response.status == 'completed':
                    print(f"Async workflow completed. Result: {status_response.result}")
                    break
                elif status_response.status == 'failed':
                    print(f"Async workflow failed. Error: {status_response.error}")
                    break
                else:
                    print(f"Async workflow status: {status_response.status}. Waiting...")
                    await asyncio.sleep(5)

        except Exception as e:
            print(f"An error occurred: {e}")

if __name__ == "__main__":
    asyncio.run(run_workflow())

view raw JSON →