Matt
Matt

Reputation: 169

How to deploy basic python functions (with packages) on Google Cloud Functions

I am attempting to deploy a basic python function which calls an api using an HTTP trigger through Google Functions (browser-editor).

Here's the function I'm trying to deploy:

import requests
import json

def call_api():

    API_URL = 'https://some-api.com/v1'
    API_TOKEN = 'some-api-token'

    result = requests.get(API_URL+"/contacts?access_token="+API_TOKEN).json()
    print(result)

call_api()

My requirements.txt contains:

requests==2.21.0

However, every time I try to deploy the function, the following error occurs:

Unknown resource type

What am I doing wrong? The function works just fine on my local machine.

Upvotes: 1

Views: 525

Answers (1)

petomalina
petomalina

Reputation: 2140

Please refer to Writing HTTP Functions for more information. This is what comes to my mind when looking at your code:

  • Missing the request parameter (def call_api(request):)
  • Missing return at the end (you don't need to print it, just return it to the caller)
  • Calling call_api() at the end of the file will call the function only locally, CFs don't need this

Make sure you are deploying using gcloud functions deploy call_api --runtime python37 --trigger-http

Upvotes: 1

Related Questions