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: 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

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 border.

Python server code with an orange bordewr

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.route('/add/:a/:b'):

@anvil.server.route('/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:

    <p><code>https://BWRDAQNJ2QGBCDYQ.anvil.app/_/private_api/KPFGAZ72GKPSZ2N4FZV5TLOV/add/32/10</code></p>

If you haven’t published your app yet, at the bottom of your Server Module you will see a message like this:

Message at bottom of Server Module, prompting you to publish your app

Once you’ve published your app, you will see 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://tepid-optimal-brain.anvil.app/api/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/KPFGAZ72GKPSZ2N4FZV5TLOV/add/1/3

https://tepid-optimal-brain.anvil.app/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.


Do you still have questions?

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