Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import os |
| 2 | |
| 3 | from composio import Composio |
| 4 | from composio.types import auth_scheme |
| 5 | |
| 6 | composio = Composio() |
| 7 | |
| 8 | # Auth config to connect against and the connecting user (raise KeyError when unset) |
| 9 | gmail_auth_config_id = os.environ["COMPOSIO_EXAMPLES_GMAIL_AUTH_CONFIG_ID"] |
| 10 | user_id = os.environ["COMPOSIO_EXAMPLES_USER_ID"] |
| 11 | |
| 12 | # List all connected accounts |
| 13 | connected_accounts = composio.connected_accounts.list() |
| 14 | print(connected_accounts) |
| 15 | |
| 16 | # Create a new connected account (OAuth). The user may already have a |
| 17 | # connected account for this auth config, so allow another one. |
| 18 | connection_request = composio.connected_accounts.initiate( |
| 19 | user_id=user_id, |
| 20 | auth_config_id=gmail_auth_config_id, |
| 21 | allow_multiple=True, |
| 22 | ) |
| 23 | |
| 24 | # Send the user to this URL to authorize the connection |
| 25 | print(f"Visit this URL to authorize: {connection_request.redirect_url}") |
| 26 | |
| 27 | # Wait for the connection to be established (OAuth) |
| 28 | connected_account = connection_request.wait_for_connection() |
| 29 | # Print identifying fields only: the full object carries live OAuth credentials. |
| 30 | print(f"Connected account {connected_account.id} is {connected_account.status}") |
| 31 | |
| 32 | # Create a new connected account (API Key) |
| 33 | connection_request = composio.connected_accounts.initiate( |
| 34 | user_id=user_id, |
| 35 | auth_config_id=os.environ["COMPOSIO_EXAMPLES_APIKEY_AUTH_CONFIG_ID"], |
| 36 | allow_multiple=True, |
| 37 | config=auth_scheme.api_key( |
| 38 | options={ # type: ignore[arg-type] # api_key() injects "status"; the field is Required on the generated TypedDict but not needed here |
| 39 | "generic_api_key": os.environ["COMPOSIO_EXAMPLES_APIKEY_PLACEHOLDER"], |
| 40 | }, |
| 41 | ), |
| 42 | ) |
| 43 | print(connection_request) |
| 44 | |
| 45 | # When creating a connected account, you can check for required fields |
| 46 | required_fields = composio.toolkits.get_connected_account_initiation_fields( |
| 47 | toolkit="NOTION", |
| 48 | auth_scheme="API_KEY", |
| 49 | ) |
| 50 | print(required_fields) |
| 51 | |
| 52 | # Retrieve a specific connected account |
| 53 | connected_account_retrieved = composio.connected_accounts.get(connected_account.id) |
| 54 | print(connected_account_retrieved) |
| 55 | |
| 56 | # Disable a connected account |
| 57 | composio.connected_accounts.disable(connected_account.id) |
| 58 | print("Connected account disabled") |
| 59 | |
| 60 | # Enable a connected account |
| 61 | composio.connected_accounts.enable(connected_account.id) |
| 62 | print("Connected account enabled") |
| 63 | |
| 64 | # Delete a connected account |
| 65 | composio.connected_accounts.delete(connected_account.id) |
| 66 | print("Connected account deleted") |