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 | import os |
| 2 | |
| 3 | from fastapi import FastAPI |
| 4 | from fastapi.responses import RedirectResponse |
| 5 | |
| 6 | from composio import Composio |
| 7 | |
| 8 | # Create a FastAPI app |
| 9 | app = FastAPI() |
| 10 | |
| 11 | # Create a Composio client |
| 12 | composio = Composio() |
| 13 | |
| 14 | |
| 15 | @app.get("/authorize/{toolkit}") |
| 16 | def authorize_app(toolkit: str): |
| 17 | # retrieve the user id from your app |
| 18 | user_id = os.environ.get("COMPOSIO_EXAMPLES_USER_ID", "") |
| 19 | |
| 20 | # retrieve the auth config id from your app |
| 21 | auth_config_id = os.environ.get("COMPOSIO_EXAMPLES_GMAIL_AUTH_CONFIG_ID", "") |
| 22 | |
| 23 | # initiate the connection request |
| 24 | connection_request = composio.connected_accounts.initiate( |
| 25 | user_id=user_id, |
| 26 | auth_config_id=auth_config_id, |
| 27 | ) |
| 28 | return RedirectResponse(url=connection_request.redirect_url) # type: ignore |
| 29 | |
| 30 | |
| 31 | if __name__ == "__main__": |
| 32 | import uvicorn |
| 33 | |
| 34 | for var in ("COMPOSIO_EXAMPLES_USER_ID", "COMPOSIO_EXAMPLES_GMAIL_AUTH_CONFIG_ID"): |
| 35 | if not os.environ.get(var): |
| 36 | raise SystemExit(f"{var} is required") |
| 37 | |
| 38 | # Fail fast on bad credentials or a stale auth config before serving requests |
| 39 | composio.auth_configs.get(os.environ["COMPOSIO_EXAMPLES_GMAIL_AUTH_CONFIG_ID"]) |
| 40 | |
| 41 | uvicorn.run(app, host="127.0.0.1", port=8000) |