@devries-cameron20 / composio Mirror · read-only / commits / ebeee43e4
Fork 0

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 23, 00:30 UTC

docs: python snippets 2 (#1634)

Co-authored-by: balyan.sid@gmail.com <balyan.sid@gmail.com>

Viraj committed Jun 14, 2025 at 00:30 UTC ebeee43e4ba5f9c98988b51d99e64138ce66e2cf
6 files changed +266 -31
fern/pages/authentication/custom-auth-configs.mdx
+2 -2
@@ -143,7 +143,7 @@ https://yourdomain.com/api/composio-redirect
143
144 </Step>
145 <Step title="Create a redirect logic">
146 -Create a redirect logic, either through your DNS or in your application to redirect that endpoint to `https://backend.composio.dev/api/v1/auth-apps/add`
146 +Create a redirect logic, either through your DNS or in your application to redirect that endpoint to `https://backend.composio.dev/api/v3/toolkits/callback`
147
148 **Example: API Route for OAuth Redirect**
149
@@ -153,7 +153,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
153
154 export default function handler(req: NextApiRequest, res: NextApiResponse) {
155 // The target Composio endpoint that handles OAuth callbacks
156 - const composioEndpoint = 'https://backend.composio.dev/api/v1/auth-apps/add';
156 + const composioEndpoint = 'https://backend.composio.dev/api/v3/toolkits/callback';
157
158 // Extract and preserve all query parameters
159 const queryParams = new URLSearchParams();
fern/pages/authentication/programmatic-auth-configs.mdx
+34 -5
@@ -52,7 +52,7 @@ You can also provide your own authentication details. The required `credentials`
52 ## Using Custom Auth
53 ### Create the auth config
54
55 -If you know the required authentication fields for your toolkit, you can directly create the auth config. OAuth2 is the most common auth type for apps. When using your own OAuth developer app, you must ensure to set the **authorized redirect URI** in the OAuth configuration to `https://backend.composio.dev/api/v1/auth-apps/add`.
55 +If you know the required authentication fields for your toolkit, you can directly create the auth config. OAuth2 is the most common auth type for apps. When using your own OAuth developer app, you must ensure to set the **authorized redirect URI** in the OAuth configuration to `https://backend.composio.dev/api/v3/toolkits/callback`.
56
57 This is the URI that captures the OAuth code that is returned to the app.
58
@@ -66,7 +66,7 @@ const authConfig = await composio.authConfigs.create("NOTION", {
66 credentials: {
67 client_id: "1234567890",
68 client_secret: "1234567890",
69 - oauth_redirect_uri: "https://backend.composio.dev/api/v1/auth-apps/add",
69 + oauth_redirect_uri: "https://backend.composio.dev/api/v3/toolkits/callback",
70 },
71 authScheme: "OAUTH2",
72 });
@@ -84,7 +84,7 @@ auth_config = composio.auth_configs.create(
84 "credentials": {
85 "client_id": "1234567890",
86 "client_secret": "1234567890",
87 - "oauth_redirect_uri": "https://backend.composio.dev/api/v1/auth-apps/add",
87 + "oauth_redirect_uri": "https://backend.composio.dev/api/v3/toolkits/callback",
88 },
89 },
90 )
@@ -111,8 +111,12 @@ const authFields = toolkits.authConfigDetails?.flatMap(
111
112 console.log("Required auth config fields:", authFields);
113 ```
114 -{/* TODO: implement method which returns required fields for a toolkit with given auth scheme */}
114 ```python Python maxLines=60 wordWrap
115 +required_fields = composio.auth_configs.get_required_fields(
116 + toolkit="NOTION",
117 + auth_scheme="OAUTH2",
118 +)
119 +print(required_fields)
120 ```
121 </CodeGroup>
122
@@ -156,8 +160,33 @@ const authConfig = await composio.authConfigs.create("NOTION", {
160 ),
161 });
162 ```
159 - {/* TODO: add once the above todo is addressed */}
163 ```python Python maxLines=60 wordWrap
164 +import os
165 +
166 +from composio import Composio
167 +
168 +composio = Composio()
169 +
170 +# Get required fields
171 +required_fields = [
172 + field.name for field in composio.auth_configs.get_required_fields(
173 + toolkit="NOTION",
174 + auth_scheme="OAUTH2",
175 + )
176 +]
177 +
178 +# Read required fields as environment variables
179 +auth_config = composio.auth_configs.create(
180 + toolkit="notion",
181 + options={
182 + "name": "Notion Auth",
183 + "type": "use_custom_auth",
184 + "auth_scheme": "OAUTH2",
185 + "credentials": {
186 + field: os.getenv(f"NOTION_{field.upper()}") for field in required_fields
187 + },
188 + },
189 +)
190 ```
191 </CodeGroup>
192 </Accordion>
\ No newline at end of file
fern/pages/tool-calling/authenticating-tools.mdx
+31 -3
@@ -177,10 +177,14 @@ detail => detail.fields.connectedAccountInitiation
177 );
178 console.log(fieldNames); // e.g. ["apiKey"]
179
180 -````
180 +```
181
182 ```python Python maxLines=60 wordWrap
183 -
183 +required_fields = composio.connected_accounts.get_required_fields(
184 + toolkit="NOTION",
185 + auth_scheme="API_KEY",
186 +)
187 +print(required_fields)
188 ````
189
190 </CodeGroup>
@@ -266,9 +270,33 @@ data: authData,
270
271 console.log("Connection request ID:", connectionRequest.id);
272
269 -````
273 +```
274 ```python Python maxLines=60 wordWrap
275 +import os
276 +from composio import Composio
277 +
278 +composio = Composio()
279
280 +# Get required fields
281 +required_fields = [
282 + field.name for field in composio.connected_accounts.get_required_fields(
283 + toolkit="NOTION",
284 + auth_scheme="API_KEY",
285 + )
286 +]
287 +print("required_fields: ", required_fields)
288 +
289 +# Create a new connected account (API Key)
290 +connection_request = composio.connected_accounts.initiate(
291 + user_id="1234567890",
292 + auth_config_id="1234567890",
293 + config=auth_scheme.api_key(
294 + options={
295 + field: os.getenv(f"NOTION_{field.upper()}") for field in required_fields
296 + },
297 + ),
298 +)
299 +print(connection_request)
300 ````
301
302 </CodeGroup>
fern/pages/tool-calling/custom-tools.mdx
+107 -4
@@ -37,8 +37,30 @@ const tool = await composio.tools.createCustomTool({
37 });
38 ```
39 ```python Python maxLines=60 wordWrap
40 +from pydantic import BaseModel, Field
41
41 -````
42 +from composio import Composio
43 +from composio.types import ExecuteRequestFn
44 +
45 +composio = Composio()
46 +
47 +
48 +class AddTwoNumbersInput(BaseModel):
49 + a: int = Field(
50 + ...,
51 + description="The first number to add",
52 + )
53 + b: int = Field(
54 + ...,
55 + description="The second number to add",
56 + )
57 +
58 +# function name will be used as slug
59 +@composio.tools.custom_tool
60 +def add_two_numbers(request: AddTwoNumbersInput) -> int:
61 + """Add two numbers."""
62 + return request.a + request.b
63 +```
64 </CodeGroup>
65
66 ### Toolkit-based Tool
@@ -86,10 +108,40 @@ const tool = await composio.tools.createCustomTool({
108 return result;
109 },
110 });
89 -````
111 +```
112
113 ```python Python maxLines=60 wordWrap
114 +class GetIssueInfoInput(BaseModel):
115 + issue_number: int = Field(
116 + ...,
117 + description="The number of the issue to get information about",
118 + )
119
120 +# function name will be used as slug
121 +@composio.tools.custom_tool(toolkit="github")
122 +def get_issue_info(
123 + request: GetIssueInfoInput,
124 + execute_request: ExecuteRequestFn,
125 + auth_credentials: dict,
126 +) -> dict:
127 + """Get information about a GitHub issue."""
128 + response = execute_request(
129 + endpoint=f"/repos/composiohq/composio/issues/{request.issue_number}",
130 + method="GET",
131 + parameters=[
132 + {
133 + "name": "Accept",
134 + "value": "application/vnd.github.v3+json",
135 + "type": "header",
136 + },
137 + {
138 + "name": "Authorization",
139 + "value": f"Bearer {auth_credentials['access_token']}",
140 + "type": "header",
141 + },
142 + ],
143 + )
144 + return {"data": response.data}
145 ```
146
147 </CodeGroup>
@@ -123,9 +175,26 @@ const tool = await composio.tools.createCustomTool({
175 },
176 });
177
126 -````
178 +```
179 ```python Python maxLines=60 wordWrap
128 -````
180 +import requests
181 +
182 +@composio.tools.custom_tool(toolkit="github")
183 +def get_issue_info_direct(
184 + request: GetIssueInfoInput,
185 + execute_request: ExecuteRequestFn,
186 + auth_credentials: dict,
187 +) -> dict:
188 + """Get information about a GitHub issue."""
189 + response = requests.get(
190 + f"https://api.github.com/repos/composiohq/composio/issues/{request.issue_number}",
191 + headers={
192 + "Accept": "application/vnd.github.v3+json",
193 + "Authorization": f"Bearer {auth_credentials['access_token']}",
194 + },
195 + )
196 + return {"data": response.json()}
197 +```
198
199 </CodeGroup>
200
@@ -179,6 +248,35 @@ const tool = await composio.tools.createCustomTool({
248 });
249 ```
250 ```python Python maxLines=60 wordWrap
251 +@composio.tools.custom_tool(toolkit="github")
252 +def get_issue_info(
253 + request: GetIssueInfoInput,
254 + execute_request: ExecuteRequestFn,
255 + auth_credentials: dict,
256 +) -> dict:
257 + """Get information about a GitHub issue."""
258 + response = execute_request(
259 + endpoint=f"/repos/composiohq/composio/issues/{request.issue_number}",
260 + method="GET",
261 + parameters=[
262 + {
263 + "name": "Accept",
264 + "value": "application/vnd.github.v3+json",
265 + "type": "header",
266 + },
267 + {
268 + "name": "Authorization",
269 + "value": f"Bearer {auth_credentials['access_token']}",
270 + "type": "header",
271 + },
272 + {
273 + "name": 'X-Custom-Header',
274 + "value": 'custom-value',
275 + "type": 'header',
276 + },
277 + ],
278 + )
279 + return {"data": response.data}
280 ```
281 </CodeGroup>
282
@@ -197,6 +295,11 @@ const result = await composio.tools.execute('TOOL_SLUG', {
295 });
296 ```
297 ```python Python maxLines=60 wordWrap
298 +response = composio.tools.execute(
299 + user_id="default",
300 + slug="TOOL_SLUG", # For the tool above you can use `get_issue_info.slug`
301 + arguments={"issue_number": 1},
302 +)
303 ```
304 </CodeGroup>
305
fern/pages/tool-calling/executing-tools.mdx
+37 -1
@@ -49,7 +49,32 @@ console.log(result);
49 ```
50
51 ```python Python (Google) maxLines=60
52 +import anthropic
53
54 +from composio import Composio
55 +from composio_anthropic import AnthropicProvider
56 +
57 +
58 +# Initialize tools.
59 +anthropic_client = anthropic.Anthropic()
60 +composio = Composio(provider=AnthropicProvider())
61 +
62 +# Get GitHub tools that are pre-configured
63 +tools = composio.tools.get(user_id="default", toolkits=["GITHUB_GET_THE_ZEN_OF_GITHUB"])
64 +
65 +# Get response from the LLM
66 +response = anthropic_client.messages.create(
67 + model="claude-3-opus-20240229",
68 + max_tokens=1024,
69 + tools=tools,
70 + messages=[
71 + {"role": "user", "content": "gimme me some zen"},
72 + ],
73 +)
74 +
75 +# Execute the function calls.
76 +result = composio.provider.handle_tool_calls(user_id="default", response=response)
77 +print(result)
78 ```
79 </CodeGroup>
80
@@ -191,7 +216,18 @@ const { data } = await composio.tools.proxyExecute({
216 console.log(data);
217 ```
218 ```python Python maxLines=60 wordWrap
194 -
219 +response = composio.tools.proxy(
220 + endpoint="/repos/composiohq/composio/issues/1",
221 + method="GET",
222 + connected_account_id="ac_1234", # use connected account for github
223 + parameters=[
224 + {
225 + "name": "Accept",
226 + "value": "application/vnd.github.v3+json",
227 + "type": "header",
228 + },
229 + ],
230 +)
231 ```
232 </CodeGroup>
233
fern/pages/triggers/using-triggers.mdx
+55 -16
@@ -52,7 +52,15 @@ const createResponse = await composio.triggers.create("GITHUB_STAR_ADDED_EVENT",
52 });
53 ```
54 ```python Python
55 -
55 +instance = composio.triggers.create(
56 + slug="GITHUB_COMMIT_EVENT",
57 + connected_account_id="ca_abc1234",
58 + trigger_config={
59 + "repo": "composio",
60 + "owner": "composiohq",
61 + },
62 +)
63 +print(instance)
64 ```
65 </CodeGroup>
66
@@ -75,7 +83,25 @@ const createResponse = await composio.triggers.create("GITHUB_STAR_ADDED_EVENT",
83 });
84 ```
85 ```python Python
78 -
86 +# Fetch connected account for user
87 +user_id = "user@acme.com"
88 +connected_accounts = composio.connected_accounts.list(user_ids=[user_id], limit=1).items
89 +if len(connected_accounts) == 0:
90 + raise ValueError("No connected account found")
91 +
92 +# Get the first connected account
93 +connected_account = connected_accounts[0]
94 +
95 +# Create a trigger instance
96 +instance = composio.triggers.create(
97 + slug="GITHUB_COMMIT_EVENT",
98 + connected_account_id=connected_account.id,
99 + trigger_config={
100 + "repo": "composio",
101 + "owner": "composiohq",
102 + },
103 +)
104 +print(instance)
105 ```
106 </CodeGroup>
107
@@ -100,7 +126,13 @@ await composio.triggers.subscribe(
126 )
127 ```
128 ```python Python
129 +# Subscribe to a trigger
130 +subscription = composio.triggers.subscribe()
131
132 +# Define a handler
133 +@subscription.handle(toolkit="GITHUB", user_id=user_id)
134 +def handle_github_event(data):
135 + print(data)
136 ```
137 </CodeGroup>
138
@@ -114,7 +146,9 @@ const triggerType = await composio.triggers.getType("GITHUB_STAR_ADDED_EVENT");
146 console.log(triggerType.payload);
147 ```
148 ```python Python
117 -
149 +# Get a trigger by id
150 +trigger = composio.triggers.get_type(slug="GITHUB_COMMIT_EVENT")
151 +print(trigger.payload)
152 ```
153 </CodeGroup>
154
@@ -131,18 +165,12 @@ The dashboard will show you the trigger configuration to fill in.
165 ```typescript TypeScript
166 const triggerType = await composio.triggers.getType("GITHUB_STAR_ADDED_EVENT");
167
134 -console.log(triggerType.config.properties);
135 -
136 -await composio.triggers.create("GITHUB_STAR_ADDED_EVENT", {
137 - connectedAccountId: "ca_1DirCHFzH7v4",
138 - triggerConfig: {
139 - owner: "composiohq",
140 - repo: "composio",
141 - },
142 -});
168 +console.log(triggerType.config);
169 ```
170 ```python Python
145 -
171 +# Get a trigger by id
172 +trigger = composio.triggers.get_type(slug="GITHUB_COMMIT_EVENT")
173 +print(trigger.config)
174 ```
175 </CodeGroup>
176
@@ -158,6 +186,14 @@ await composio.triggers.create("GITHUB_PULL_REQUEST_EVENT", {
186 });
187 ```
188 ```python Python
189 +instance = composio.triggers.create(
190 + slug="GITHUB_PULL_REQUEST_EVENT",
191 + connected_account_id="ca_abc1234",
192 + trigger_config={
193 + "repo": "composio",
194 + "owner": "composiohq",
195 + },
196 +)
197 ```
198 </CodeGroup>
199
@@ -170,7 +206,9 @@ In cases where the trigger isn't required for the system, it can be disabled.
206 await composio.triggers.disable("ti_So9EQf8XnAcy");
207 ```
208 ```python Python
173 -
209 +# Disable a trigger instance
210 +disabled_instance = composio.triggers.disable(trigger_id="ti_So9EQf8XnAcy")
211 +print(disabled_instance)
212 ```
213 </CodeGroup>
214
@@ -181,7 +219,8 @@ If needed, the trigger can be enabled again.
219 await composio.triggers.enable("ti_So9EQf8XnAcy");
220 ```
221 ```python Python
184 -
222 +# Enable a trigger instance
223 +enabled_instance = composio.triggers.enable(trigger_id="ti_So9EQf8XnAcy")
224 +print(enabled_instance)
225 ```
226 </CodeGroup>
187 -