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

Quickstart: Making HTTP requests

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

Location of the Create App button

Install requests

Go to Settings, and click on Python versions. Select “Python 3.10” from the dropdown and install requests.

Add a Server Module

Go back to the App Browser and click the + next to Server Code 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

Go back to the App Browser and click “+ Add Server Module” next to Server Code to add a new Server Module.

Adding a Server Module in the App Browser

A new tab will open with a code editor with an orange background.

Python server code with an orange background

Write a server function

Import requests and write this function into the Server Module:

@anvil.server.callable
def access_xkcd_api(comic_number):
  response = requests.get(f"https://xkcd.com/{comic_number}/info.0.json")
  return response.json()

requests 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

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

Run button in the editor

The Output Panel should display this:

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

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

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.


Do you still have questions?

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