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

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

Location of the Create App button

Add a Server Module

In the App Browser, 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

In the App Browser, 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

Write this function into the Server Module:

@anvil.server.callable
def access_xkcd_api(comic_number):
  url = f"https://xkcd.com/{comic_number}/info.0.json"
  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

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

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.


Do you still have questions?

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