HTTP APIs

You don’t need to make HTTP requests to build an Anvil app thanks to Anvil’s ability to call Python functions between client and server code (including code outside of Anvil). Even if you’re collaborating with a third-party, they can use the Client Uplink to safely make function calls to and from your app.

That said, HTTP is still a powerful tool to have available. Anvil has an HTTP requests library, plus a simple way to create HTTP endpoints that expose your app’s functionality over HTTP.

Making HTTP Requests

Anvil has a human-friendly Python library for making HTTP requests. This works from client and server code.

import anvil.http
resp = anvil.http.request("https://api.mysite.com/foo")
print("Response MIME type: " + resp.content_type)

See Making HTTP Requests for details.

Creating HTTP APIs

You can make your server functions into HTTP endpoints using a decorator:

import anvil.server

@anvil.server.http_endpoint("/users/:id")
def get_user(id):
  ip = anvil.server.request.remote_address
  return "You requested user %s from IP %s" % (id, ip)

See Creating HTTP Endpoints for details.