Live Chat

We'll need to share your messages (and your email address if you're logged in) with our live chat provider, Drift. Here's their privacy policy.

If you don't want to do this, you can email us instead at contact@anvil.works.

Quickstart: Making HTTP requests

Integrate with third-party HTTP APIs

Anvil lets you make HTTP requests with very little code.

Follow this quickstart to access an external HTTP API from Python and print the response.

Create an app

Log in to Anvil and click ‘New Blank App’. Choose the Material Design theme.

Location of the Create App button

Add a Server Module

In the App Browser, click the + next to Server Modules to add a new Server Module.

Adding a Server Module in the App Browser

You will see a code editor with a yellow background.

Python code with a yellow background

Write a server function

Write this function into the Server Module:

@anvil.server.callable
def access_xkcd_api(comic_number):
  url = "https://xkcd.com/{}/info.0.json".format(comic_number)
  response = anvil.http.request(url, json=True)
  return response

anvil.http.request makes the HTTP request. We are using the API of the webcomic XKCD to get the details of a given comic.

Call your server function from the client

Go to the code for Form1. It looks like this:

Code View for Form1

At the end of the __init__ method, write these lines:

    return_value = anvil.server.call('access_xkcd_api', 353)
    print(return_value)

This means your function will run when the app starts, and the result will be printed.

Run your app

Now click the ‘Run’ button at the top of the screen.

Running your app in the editor

The Output Panel should display this:

The Output Panel showing the XKCD API's rendering of the comic about Python

This is the data presented at https://xkcd.com/353/info.0.json. It has automatically been converted to a Python dictionary because we used json=True in anvil.http.request.

Copy the example app

Click on the button below to clone a finished version of this app into your account.

Next up

Want more depth on this subject?

Read more about HTTP APIs and requests in Anvil.

Want another quickstart?

Every quickstart is on the Quickstarts page.