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.

anvil.server Module

Classes

CallContext

Create a new ‘CallContext’ object


CallContext Attributes

client - anvil.server.CallContext.ClientInfo instance

An object that describes the client that initiated the current session. This can be a browser, an HTTP endpoint request, an uplink script, a background task, or an incoming email.

remote_caller - anvil.server.CallContext.StackFrame instance

An object describing the code that called this @anvil.server.callable function, where it was running, and whether it is trusted (server-side) or un-trusted (input from a browser, HTTP or other remote code)

type - string

The execution environment this code is running in. May be ‘browser’, ‘server_module’ or ‘uplink’


ClientInfo

Create a new ‘CallContext.ClientInfo’ object


ClientInfo Attributes

ip - string

The IP address of the client that initiated this session.

location - anvil.server.CallContext.Location instance

The location of this client, as determined by its IP address, or None if it cannot be determined.

type - string

How this session was initiated. Valid values are: ‘browser’, ‘uplink’, ‘http’, ‘background_task’ and ‘email’.


Location

Create a new ‘CallContext.Location’ object


Location Attributes

city - string

country - string

latitude - float

longitude - float

subdivision - string


StackFrame

Create a new ‘CallContext.StackFrame’ object


StackFrame Attributes

is_trusted - boolean

Was this code running in a trusted location (ie on the server side)?

type - string

The location of the calling code. Valid values are: ‘browser’, ‘server_module’, ‘uplink’ and ‘client_uplink’ for your app’s code, or ‘http’, ‘background_task’ or ‘email’ if this code was extrernally triggered.


Capability

Create a Capability object. Give it a list representing its scope - eg [“my_database”, “table_name”]. The scope of user-created Capabilities may not begin with “anvil”.

Constructor

Capability(scope)

Instance Methods

narrow(additional_scope) → anvil.server.Capability instance

Return a new capability that is narrower than this one, by appending additional scope element(s) to it.


Capability Attributes

scope - list

A list representing what this capability represents. It can be extended by calling narrow(), but not shortened.

Eg: [‘my_resource’, 42, ‘foo’]


HttpRequest

Create a new ‘HttpRequest’ object

Constructor

HttpRequest()

HttpRequest Attributes

body - anvil.Media instance

The body of the HTTP request

body_json - dict

The decoded JSON body of the HTTP request, if applicable. Only available when Content-Type header is ‘application/json’.

form_params - dict

A dict of form parameters passed with this request.

headers - dict

HTTP headers sent with the current request

method - string

The HTTP method of the current request, e.g. GET, POST, etc.

origin - string

The origin of the current API request, e.g. https://my-app.anvil.app/_/api

password - string

The password received through HTTP Basic Authentication

path - string

The path of the current request, e.g. /foo/bar

query_params - dict

A dict of query-string parameters passed with this request.

remote_address - string

The IP address the current request is coming from.

user - User

When require_auth is True, returns the row from the Users table corresponding to the authenticated user.

username - string

The username received through HTTP Basic Authentication


HttpResponse

Create a new ‘HttpResponse’ object

Constructor

HttpResponse()

HttpResponse Attributes

body - any

The body of this HTTP response. Can be a string, a Media object, or any JSON-able value.

headers - dict

The headers to return with this HTTP response. Content-Type will be set automatically if not specified.

status - number

The status code for this HTTP response. Default is 200.


Functions

get_api_origin([branch]#) → string

Returns the root URL of the API for the current app.


get_app_origin([branch]) → string

Returns the root URL of the current app.


reset_session()

Reset the current session to prevent further SessionExpiredErrors.


Globals

context

Contains information about what triggered the currently running code.


no_loading_indicator

Use with anvil.server.no_loading_indicator: to suppress the loading indicator when making server calls


request

Contains information about the current HTTP API request.


Exceptions

BackgroundTaskError

Create a new ‘BackgroundTaskError’ object


BackgroundTaskKilled

Create a new ‘BackgroundTaskKilled’ object


BackgroundTaskNotFound

Create a new ‘BackgroundTaskNotFound’ object


ExecutionTerminatedError

Create a new ‘ExecutionTerminatedError’ object


InternalError

Create a new ‘InternalError’ object


InvalidResponseError

Create a new ‘InvalidResponseError’ object


NoServerFunctionError

Create a new ‘NoServerFunctionError’ object


QuotaExceededError

Create a new ‘QuotaExceededError’ object


RuntimeUnavailableError

Create a new ‘RuntimeUnavailableError’ object


SerializationError

Create a new ‘SerializationError’ object


SessionExpiredError

Create a new ‘SessionExpiredError’ object


TimeoutError

Create a new ‘TimeoutError’ object


UplinkDisconnectedError

Create a new ‘UplinkDisconnectedError’ object