Is there an easy way to create an API in Anvil?
For my purposes I just want to be able to call a server function (somehow) from ANOTHER Anvil App with (or even without) authentication? Is there some easy way to wrap a call like that?
Is there an easy way to create an API in Anvil?
For my purposes I just want to be able to call a server function (somehow) from ANOTHER Anvil App with (or even without) authentication? Is there some easy way to wrap a call like that?
You can add any app as dependency and then use forms or modules from that app.
You can create an http_endpoint and call it as any other http API.
You can create an uplink module in your own computer and use it as an API server for your app.
You can create an uplink module in your own computer and use it to call functions defined on your app. Even better, it can access database tables and other app resources directly, without writing any code in the server side.
For your case, I think you want to use an app as a depedency.
Let’s say you have a function in AppA that you want to use in AppB. It can be a client function or a server function.
In AppA: Click on ‘Dependencies’ in the cogwheel menu. The bottom part of the modal that comes up says ‘Use this app as a library’. You can specify what you want your package to be named in the code of the importing app.
In AppB: Click on ‘Dependencies’ in the cogwheel menu. The top part of the modal says ‘Use other apps as libraries’. You can select your app in the dropdown.
Then all you need to do is import AppA
in your code and you’ll have access to everything, e.g. AppA.Form1.do_something()
.
Just for future reference, there definitely is an easy way to create HTTP APIs.
Here’s the reference docs for HTTP APIs:
https://anvil.works/doc#-div-id-http_apis-http-apis-div-
You decorate a server function with the @anvil.server.http_endpoint
decorator. You can require the user to use Basic Auth and the return value is the response.
@anvil.server.http_endpoint("/foo", authenticate_users=True)
def serve_content():
response = anvil.server.HttpResponse(200, "Body goes here")
response.headers["X-Custom-Header"] = "Custom value"
return response