Custom Action for Serphouse.com python serpcrawler API

Hello,

I’m trying to do simple action which will use Serphouse.com API and which get serp results for defined keyword, but im still getting errors. CAn you tell my what is wrong with that code?

import requests
url = "https://api.serphouse.com/serp/live"
payload = '{"data":{
			"q": "Hosting",
			"domain": "google.com",
			"loc": "Abernathy,Texas,United States",
			"lang": "en",
			"device": "desktop",
			"serp_type": "web",
			"page": "1",
			"verbatim": "0"
}}'
headers = {
    'accept': "application/json",
    'content-type': "application/json",
    'authorization': "Bearer XXX"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)

Hey @suprime

1st thing I noticed is you didn’t enter your API key.

  • Ensure that 'authorization': "Bearer XXX" contains your actual API key in place of "XXX".
  • Example: 'authorization': "Bearer your_actual_api_key"
  • You need a JSON-formatted payload string.
  • Verify that the location (referred to as “loc” in the code) "Abernathy,Texas,United States" is correctly formatted and recognized by the API.

Here is the code with guidance on where to place your API key:

import requests

url = "https://api.serphouse.com/serp/live"
payload = {
    "data": {
        "q": "Hosting",
        "domain": "google.com",
        "loc": "Abernathy,Texas,United States",
        "lang": "en",
        "device": "desktop",
        "serp_type": "web",
        "page": "1",
        "verbatim": "0"
    }
}
headers = {
    'accept': "application/json",
    'content-type': "application/json",
    'authorization': "Bearer your_actual_api_key"
}

response = requests.post(url, json=payload, headers=headers)

if response.status_code == 200:
    print(response.json())
else:
    print(f"Error {response.status_code}: {response.text}")

You can find your API key in your Serphouse project dashboard… usually, API access comes with a premium subscription.

hey, i just remove my api here for forum :D.

i replaced code in action and add there my api key

My code:

import os
import requests

def serphouse_api_run():
    """
    Get results of api serphouse.com run for defined ketowrd

    """

    # Insert your PYTHON code below. You can access environment variables using os.environ[].
    # Currently, only the requests library is supported, but more libraries will be available soon.
    # Use print statements or return values to display results to the user.
    # If you save a png, pdf, csv, jpg, webp, gif, or html file in the root directory, it will be automatically displayed to the user.
    # You do not have to call this function as the bot will automatically call and fill in the parameters.

    url = "https://api.serphouse.com/serp/live"
    payload = {
        "data": {
            "q": "Hosting",
            "domain": "google.com",
            "loc": "Abernathy,Texas,United States",
            "lang": "en",
            "device": "desktop",
            "serp_type": "web",
            "page": "1",
            "verbatim": "0"
        }
    }
    headers = {
        'accept': "application/json",
        'content-type': "application/json",
        'authorization': "Bearer I PUT HERE MY API"
    }
    
    response = requests.post(url, json=payload, headers=headers)
    
    if response.status_code == 200:
        print(response.json())
    else:
        print(f"Error {response.status_code}: {response.text}")

I even try code similar to Dalee:

import os
import requests

def serphouse_api_run(kewyord_query: str):
    """
    Allow pickaxe to get results from api serphouse.com for user defined keyword

    Args:
        kewyord_query (string): Prompt for the keyword to get API results
    """

    # Insert your PYTHON code below. You can access environment variables using os.environ[].
    # Currently, only the requests library is supported, but more libraries will be available soon.
    # Use print statements or return values to display results to the user.
    # If you save a png, pdf, csv, jpg, webp, gif, or html file in the root directory, it will be automatically displayed to the user.
    # You do not have to call this function as the bot will automatically call and fill in the parameters.

    api_key = os.environ["XX"]
    headers = {
                    "Content-Type": "application/json",
                }
    headers["Authorization"] = f"Bearer {api_key}"

    url = "https://api.serphouse.com/serp/live"
    payload = {
        "data": {
            "q": kewyord_query,
            "domain": "google.com",
            "loc": "Abernathy,Texas,United States",
            "lang": "en",
            "device": "desktop",
            "serp_type": "web",
            "page": "1",
            "verbatim": "0"
        }
    }
    headers = {
        'accept': "application/json",
        'content-type': "application/json",
        'authorization': "Bearer I PUT HERE MY API"
    }
    
    response = requests.post(url, json=payload, headers=headers)
    
    if response.status_code == 200:
        print(response.json())
    else:
        print(f"Error {response.status_code}: {response.text}")

And it still crashing

Got it :smiley:

What do you mean by “you tried code similar to Dalee”?

response = requests.post(url, json=payload, headers=headers)

if response.status_code == 200:
    print(response.json())
else:
    print(f"Error {response.status_code}: {response.text}")

The code section above is supposed to let you know what the error was by pointing it out. Can you get me the error code/message?

Thx

Hey,

my current code is:

import os
import requests

def serphouse_api_run(keyword_query: str):
    """
    Allow pickaxe to get results from api serphouse.com for user defined keyword

    Args:
        keyword_query (string): Prompt for the keyword to get API results
    """

    # Insert your PYTHON code below. You can access environment variables using os.environ[].
    # Currently, only the requests library is supported, but more libraries will be available soon.
    # Use print statements or return values to display results to the user.
    # If you save a png, pdf, csv, jpg, webp, gif, or html file in the root directory, it will be automatically displayed to the user.
    # You do not have to call this function as the bot will automatically call and fill in the parameters.

    url = "https://api.serphouse.com/serp/live"
    payload = {
        "data": {
            "q": "Hosting",
            "domain": "google.com",
            "loc": "Abernathy,Texas,United States",
            "lang": "en",
            "device": "desktop",
            "serp_type": "web",
            "page": "1",
            "verbatim": "0"
        }
    }
    headers = {
        'accept': "application/json",
        'content-type': "application/json",
        'authorization': "Bearer XXX(removed here)"
    }
    
    response = requests.post(url, json=payload, headers=headers)
    
    if response.status_code == 200:
        print(response.json())
    else:
        print(f"Error {response.status_code}: {response.text}")

Here is my action config:

When i test prompt i receive:

​Proszę czekać, trwa pobieranie danych dla słowa kluczowego ‘dentysta’.

Image build for im-EabkG2xzYjYFpZgX3OIrOs failed with the exception: task exited with failure, status = exit status: 1