You are currently viewing the new Anvil Editor Docs.
Switch to the Classic Editor Docs
You are currently viewing the Classic Editor Docs.
Switch to the new Anvil Editor Docs

Google REST APIs

You might want to use one of Google’s REST APIs directly. To do this, you will need a Google API Client ID from the Google Developer Console - see Linking Anvil and Google for full instructions.

To request additional OAuth scopes, call with a list of scope names. In this case we are requesting the ability to read the user’s Gmail inbox with the Gmail API.

# Log in and request access to a user's mailbox[""])

Once logged in, you can get an access token for the current user’s session by calling This token is a string, and you can use it to make requests to the Google REST API. In this example, we are getting a list of threads in the user’s inbox using the Gmail API.

access_token =

email_threads = anvil.http.request(
    json=True, headers={
        'Bearer ' + access_token

Access tokens expire within a short period of time (typically less than an hour). If you want to retain access after this time, you need to store a refresh token. This refresh token can be converted into an access token later by calling

refresh_token =

# ... some time later ...

access_token =

Do you still have questions?

Our Community Forum is full of helpful information and Anvil experts.