Problems with programatically connecting an account

I want to programatically connect an account. I know this can be done with composio add <account>, but I want to do this programatically. I have the following code:
from composio_crewai import Action, App
import streamlit as st
import json

def initialize_serpapi(composio_client, serpapi_api_key):
try:
entity = composio_client.get_entity()

try:
entity.get_connection(app=App.SERPAPI)
except Exception as e:

auth_config = {"api_key": serpapi_api_key}

connection_request = entity.initiate_connection(
app_name=App.SERPAPI,
auth_mode="api_key",
auth_config=auth_config,
)

connected_account = connection_request.wait_until_active(
client=composio_client.client,
)

if not connected_account:
st.error(
body="Failed to establish a connection with SerpApi tool",
icon="❌",
)
return None

serpapi_tool = composio_client.get_tools(
actions=[Action.SERPAPI_SEARCH],
)
return serpapi_tool

except Exception as e:
try:
error_data = json.loads(str(e))

status = error_data.get("status")
message = error_data.get("message")

st.error(
body=f"Error initializing SerpApi tool\n\nStatus: {status}\n\nMessage: {message}",
icon="❌",
)
return None
except json.JSONDecodeError:
st.error(
body=f"Error initializing SerpApi tool\n\n{e}",
icon="❌",
)
return None
from composio_crewai import Action, App
import streamlit as st
import json

def initialize_serpapi(composio_client, serpapi_api_key):
try:
entity = composio_client.get_entity()

try:
entity.get_connection(app=App.SERPAPI)
except Exception as e:

auth_config = {"api_key": serpapi_api_key}

connection_request = entity.initiate_connection(
app_name=App.SERPAPI,
auth_mode="api_key",
auth_config=auth_config,
)

connected_account = connection_request.wait_until_active(
client=composio_client.client,
)

if not connected_account:
st.error(
body="Failed to establish a connection with SerpApi tool",
icon="❌",
)
return None

serpapi_tool = composio_client.get_tools(
actions=[Action.SERPAPI_SEARCH],
)
return serpapi_tool

except Exception as e:
try:
error_data = json.loads(str(e))

status = error_data.get("status")
message = error_data.get("message")

st.error(
body=f"Error initializing SerpApi tool\n\nStatus: {status}\n\nMessage: {message}",
icon="❌",
)
return None
except json.JSONDecodeError:
st.error(
body=f"Error initializing SerpApi tool\n\n{e}",
icon="❌",
)
return None
I get the following error in the browser when viewing the Streamlit app:
Error initializing SerpApi tool

Status: 404

Message: The requested resource was not found
Error initializing SerpApi tool

Status: 404

Message: The requested resource was not found
What's wrong with my code? The error message doesn't tell me much.
15 Replies
genetic-orange
genetic-orange2w ago
Checking @xaxbxc_
genetic-orange
genetic-orange2w ago
@thatsmeadarsh I currently have no existing SerpApi connection, so the code should generate one. Instead, I'm getting the mentioned error.
genetic-orange
genetic-orange2w ago
Yea yea I got it, I am seeing it and letting you know
genetic-orange
genetic-orange2w ago
@thatsmeadarsh Thanks! What's the status on this?
genetic-orange
genetic-orange2w ago
I have shared the issue with the team letting you know asap with the status
genetic-orange
genetic-orange2w ago
@thatsmeadarsh Sorry for being a bit pushy, but I want to use Composio for a hackathon. So, I'm in a hurry, and I don't want to ditch Composio.
correct-apricot
correct-apricot2w ago
@xaxbxc_ Looking into it. Can you try this one? Otherwise, let's get on a quick call? Found the issue, fixing it. Please run pip install -U composio-crewai to install composio-crewai==0.5.17 And change the api_key code to API_KEY as auth mode. This should fix the issue.
genetic-orange
genetic-orange2w ago
@kaavee Yessssssss, it's working now. Thanks! @kaavee I'm getting an invalid API SerpApi key error, although it should be valid.
[2024-09-11 12:01:59,230][INFO] Got response={'successfull': False, 'data': {'status_code': 401, 'message': '{\n "error": "Invalid API key. Your API key should be here: https://serpapi.com/manage-api-key"\n}'}, 'error': '401 Client Error: Unauthorized for url: https://serpapi.com/search...
[2024-09-11 12:01:59,230][INFO] Got response={'successfull': False, 'data': {'status_code': 401, 'message': '{\n "error": "Invalid API key. Your API key should be here: https://serpapi.com/manage-api-key"\n}'}, 'error': '401 Client Error: Unauthorized for url: https://serpapi.com/search...
The API key in the Composio "Connected account" dashboard is identical to the one in the SerpApi dashboard. @kaavee If I use the Composio dashboard to connect SerpApi and provide the API key there, the code doesn't throw the error. Did I do anything wrong when passing the API key programatically?
connection_request = entity.initiate_connection(
app_name=App.SERPAPI,
auth_mode="API_KEY",
auth_config={"api_key": serpapi_api_key},
)
connection_request = entity.initiate_connection(
app_name=App.SERPAPI,
auth_mode="API_KEY",
auth_config={"api_key": serpapi_api_key},
)
@thatsmeadarsh I figured out what's the problem. If I do it programatically, the API key is not passed in the Headers (I see {"api_key":""}), although the "API KEY" above is valid. I should see {"api_key":"xxxxxxxxxxxxxxxxxxxxx"}. Whereas if I do it using the Composio dashboard, I see {"api_key":"xxxxxxxxxxxxxxxxxxxxx"} and the code works. Why is that? @thatsmeadarsh @kaavee I think something is wrong on your side since I see the API key if I print it using st.write() as follows:
st.write(serpapi_api_key)

connection_request = entity.initiate_connection(
app_name=App.SERPAPI,
auth_mode="API_KEY",
auth_config={"api_key": serpapi_api_key},
)
st.write(serpapi_api_key)

connection_request = entity.initiate_connection(
app_name=App.SERPAPI,
auth_mode="API_KEY",
auth_config={"api_key": serpapi_api_key},
)
correct-apricot
correct-apricot2w ago
What's your email id?
genetic-orange
genetic-orange2w ago
Is the email ID required or what do you mean?
correct-apricot
correct-apricot2w ago
No, just want to check the logs. The email id you registered with on Composio.
genetic-orange
genetic-orange2w ago
Where do I get it?
correct-apricot
correct-apricot2w ago
On your profile pix
No description
correct-apricot
correct-apricot2w ago
You need to change the code to auth_config = {"apikey": serpapi_api_key} api_key -> apikey To get which field to be filled for future, use: app = composio_client.client.apps.get(name=str(App.SERPAPI)) Output
App name='serpapi' key='serpapi' appId='9826af81-71be-4381-9aa4-ac3d750973c4' description='SerpAPI allows for easy integration with search engine results parsing.' categories=['SEO', 'data analysis', 'popular'] meta={'triggersCount': 0, 'actionsCount': 10, 'is_custom_app': False} logo='https://raw.githubusercontent.com/SamparkAI/open-logos/master/serpapi.png' docs='' group=None status='' enabled=False no_auth=False auth_schemes=[AppAuthScheme(scheme_name='serpapi_auth', auth_mode='API_KEY', fields=[AuthSchemeField(name='apikey', description='Your SERP API key.', type='string', display_name='API Key', required=True, expected_from_customer=True)], proxy={'base_url': 'https://serpapi.com/search', 'headers': {'api_key': '{{apikey}}'}}, authorization_url=None, token_url=None, default_scopes=None, token_response_metadata=None, client_id=None, client_secret=None)] testConnectors=[] documentation_doc_text='' configuration_docs_text=''
App name='serpapi' key='serpapi' appId='9826af81-71be-4381-9aa4-ac3d750973c4' description='SerpAPI allows for easy integration with search engine results parsing.' categories=['SEO', 'data analysis', 'popular'] meta={'triggersCount': 0, 'actionsCount': 10, 'is_custom_app': False} logo='https://raw.githubusercontent.com/SamparkAI/open-logos/master/serpapi.png' docs='' group=None status='' enabled=False no_auth=False auth_schemes=[AppAuthScheme(scheme_name='serpapi_auth', auth_mode='API_KEY', fields=[AuthSchemeField(name='apikey', description='Your SERP API key.', type='string', display_name='API Key', required=True, expected_from_customer=True)], proxy={'base_url': 'https://serpapi.com/search', 'headers': {'api_key': '{{apikey}}'}}, authorization_url=None, token_url=None, default_scopes=None, token_response_metadata=None, client_id=None, client_secret=None)] testConnectors=[] documentation_doc_text='' configuration_docs_text=''
Here the field name is apikey.
genetic-orange
genetic-orange2w ago
@kaavee Thanks, it works!