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.
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.
You will see a code editor 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.
A new tab will open with a code editor 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:
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.
The Output Panel should display this:
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.