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: Creating HTTP endpoints

Put an HTTP API on your app

Anvil lets you set up HTTP endpoints with very little code.

Follow this quickstart to create an HTTP endpoint that extracts two variables from the path and creates a response dynamically based on their values.

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

Create an HTTP endpoint

Write this function:

def add_numbers(a, b):
  a = int(a)
  b = int(b)
  return {
    'originals': [a, b],
    'sum': a + b,
  }

It takes two numbers and returns a data structure containing their sum.

Above this function, write @anvil.server.http_endpoint('/add/:a/:b'):

@anvil.server.http_endpoint('/add/:a/:b')
def add_numbers(a, b):
  a = int(a)
  b = int(b)
  return {
    'originals': [a, b],
    'sum': a + b,
  }

Access your HTTP endpoint

At the bottom of your Server Module, there is a message like this:

Message at bottom of Server Module explaining URL stem of HTTP API

Copy the URL without the {path} part, and put add/32/10 at the end. For my app that would be:

https://BWRDAQNJ2QGBCDYQ.anvil.app/_/private_api/KPFGAZ72GKPSZ2N4FZV5TLOV/add/32/10

Open a new browser tab and access that URL. You should see this response:

{"sum":42,"originals":[32,10]}

Pass in different numbers to get a different answer:

https://BWRDAQNJ2QGBCDYQ.anvil.app/_/private_api/KPFGAZ72GKPSZ2N4FZV5TLOV/add/1/3

This time the response is:

{"sum":4,"originals":[1,3]}

(To set up a nicer URL for your API, see deployment.)

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.