2026-08-11 19:45:58 +02:00
|
|
|
import os
|
|
|
|
|
|
2025-06-17 12:58:00 +05:30
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from fastapi.responses import RedirectResponse
|
|
|
|
|
|
|
|
|
|
from composio import Composio
|
|
|
|
|
|
|
|
|
|
# Create a FastAPI app
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
# Create a Composio client
|
|
|
|
|
composio = Composio()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/authorize/{toolkit}")
|
|
|
|
|
def authorize_app(toolkit: str):
|
|
|
|
|
# retrieve the user id from your app
|
2026-08-11 19:45:58 +02:00
|
|
|
user_id = os.environ.get("COMPOSIO_EXAMPLES_USER_ID", "")
|
2025-06-17 12:58:00 +05:30
|
|
|
|
|
|
|
|
# retrieve the auth config id from your app
|
2026-08-11 19:45:58 +02:00
|
|
|
auth_config_id = os.environ.get("COMPOSIO_EXAMPLES_GMAIL_AUTH_CONFIG_ID", "")
|
2025-06-17 12:58:00 +05:30
|
|
|
|
|
|
|
|
# initiate the connection request
|
|
|
|
|
connection_request = composio.connected_accounts.initiate(
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
auth_config_id=auth_config_id,
|
|
|
|
|
)
|
2025-06-25 10:37:09 +05:30
|
|
|
return RedirectResponse(url=connection_request.redirect_url) # type: ignore
|
2026-08-11 19:45:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
import uvicorn
|
|
|
|
|
|
|
|
|
|
for var in ("COMPOSIO_EXAMPLES_USER_ID", "COMPOSIO_EXAMPLES_GMAIL_AUTH_CONFIG_ID"):
|
|
|
|
|
if not os.environ.get(var):
|
|
|
|
|
raise SystemExit(f"{var} is required")
|
|
|
|
|
|
|
|
|
|
# Fail fast on bad credentials or a stale auth config before serving requests
|
|
|
|
|
composio.auth_configs.get(os.environ["COMPOSIO_EXAMPLES_GMAIL_AUTH_CONFIG_ID"])
|
|
|
|
|
|
|
|
|
|
uvicorn.run(app, host="127.0.0.1", port=8000)
|