Best answer

Please define output or return early

  • 11 March 2023
  • 2 replies
  • 137 views

Userlevel 1

I am trying to make a query to an external API (weatherstack) and in the test, Zapier returns the error 'Please define output or return early'. 

I've tried many things, but I don't know what's wrong 😥

Below is the code I am using in 'Code by zapier':

import requests

def query_time(input_data):
    # Take latitude and longitude values from Zapier input.
    lat = input_data['latitude']]
    lon = input_data['longitude']]

    # Take the date from the Zapier input
    date = input_data['date']]

    # Set the default time
    start_time = '06:00:00' 
    end_time = '23:59:59' 

    # Combine date and time to create time variables
    start_time = f'{date}T{start_time}'
    end_time = f'{date}T{end_time}'

    # Set query parameters
    base_url = 'http://api.weatherstack.com/historical'
    api_key = 'API_KEY'

    # Make the API query
    url = f'{base_url}?access_key={api_key}&query={lat},{lon}&historical_date={date}&hourly=1'
    response = requests.get(url)

    # extract the parameters you need from the response
    if response.ok:
        data = response.json()
        hourly_data = data['historical'][date]
        temp_min = float('inf')
        temp_max = float('-inf')
        temp_sum = 0
        count = 0
        for hourly in hourly_data:
            time = hourly['time']
            if start_time <= time <= end_time:
                temp = hourly['temperature']
                temp_min = min(temp_min, temp)
                temp_max = max(temp_max, temp)
                temp_sum += temp
                count += 1
        temp_avg = round(temp_sum / count, 2)

        # Print the results of the query
        print(f'Maximum temperature: {temp_max}°C')
        print(f'Minimum temperature: {temp_min}°C')
        print(f'Average temperature: {temp_avg}°C')
    else:
        print('Error in making API query')

    # Define the output fields and their values
    output = {
        'temp_max': temp_max,
        'temp_min': temp_min,
        'temp_avg': temp_avg
    }

    # Return the results of the query
    return output

 

Thank you very much for your time 🤗

icon

Best answer by Troy Tessalone 12 March 2023, 19:00

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 7
Badge +14

Hi @polmarza 

Good question.

Perhaps try using the Webhooks app as an action step to make the API request.

https://zapier.com/apps/webhook/integrations

Userlevel 1

Thank you for your reply @Troy Tessalone . I finally changed the approach to simplify the process. Thanks for your time anyway.