Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | """ |
| 2 | Tool Router - Authorization Example |
| 3 | |
| 4 | This example demonstrates how to authorize a toolkit connection |
| 5 | within a Tool Router session. |
| 6 | """ |
| 7 | |
| 8 | import os |
| 9 | |
| 10 | from composio import Composio |
| 11 | |
| 12 | # Initialize Composio |
| 13 | # Set COMPOSIO_API_KEY environment variable or pass api_key parameter |
| 14 | api_key = os.environ.get("COMPOSIO_API_KEY") |
| 15 | if not api_key: |
| 16 | print("Error: COMPOSIO_API_KEY environment variable not set") |
| 17 | print("Please set it using: export COMPOSIO_API_KEY='your_api_key'") |
| 18 | exit(1) |
| 19 | |
| 20 | composio = Composio(api_key=api_key) |
| 21 | |
| 22 | # Create a tool router session |
| 23 | session = composio.create( |
| 24 | user_id="user_123", |
| 25 | toolkits=["github", "gmail"], |
| 26 | ) |
| 27 | |
| 28 | print(f"Session created: {session.session_id}") |
| 29 | |
| 30 | # Authorize a toolkit connection |
| 31 | # This initiates the OAuth flow and returns a connection request |
| 32 | connection_request = session.authorize("github") |
| 33 | |
| 34 | print(f"\nConnection Request ID: {connection_request.id}") |
| 35 | print(f"Status: {connection_request.status}") |
| 36 | print(f"Redirect URL: {connection_request.redirect_url}") |
| 37 | print("\nPlease visit the URL above to authorize the connection.") |
| 38 | |
| 39 | # Wait for the user to complete the authorization |
| 40 | # This will poll the API until the connection is active or timeout occurs |
| 41 | try: |
| 42 | connected_account = connection_request.wait_for_connection() |
| 43 | print("\n✅ Connection successful!") |
| 44 | print(f"Connected Account ID: {connected_account.id}") |
| 45 | print(f"Status: {connected_account.status}") |
| 46 | except Exception as e: |
| 47 | print(f"\n❌ Connection failed: {e}") |