Using Anvil APIs from an external app

HI,
I am trying to make a mobile app (using thunkable) that can access Anvil database, can you please share an example of how to create an API that allows an external app to access Anvil database?

I believe that you will first need to create an Anvil app.

1 Like

I have created an anvil app with database base and I saw the API documentation page, but; my problem is that I don’t know how to get the link/API key that I can use to connect with the mobile app!

I did, but I can’t find an example that shows how to do so…

It is not possible to create a connection and use sql.

You need to create a few http endpoints that will do the job for you.

1 Like

How to create the link that I can use for the API, in the documentation it gave this example:
https://<your-app-id>.anvil.app/_/private_api/<your private access key>/…
I am not really sure how to get to this point!

Create a server module, in it create your function, put the @anvil.server.http_endpoint decorator before the function and you will see the address at the bottom of the online editor (unless there are syntax errors). Click on it and press ctrl+C to copy it to the clipboard.

1 Like

Sorry, in my previous post I forgot to tell you that you cannot create a classic sql connection, but you can create an uplink connection. Look it up, it is very flexible and it can do much more.

1 Like

This is true for the in-built datatables unless you have a dedicated Anvil server plan, but you can use libraries like PyMySQL to connect to external databases.

1 Like

The question is about using the Anvil database from outside, not vice versa. And now that I reread it, from Android, so the uplink I suggested earlier is not an option.

But I agree, a solution could be to use an external database service which comes with its own APIs.

1 Like

Hi @adhamfaisal,

If you’re writing a mobile app using a language like Java, Swift of Objective-C, then yes, the best way is to use Anvil to create HTTP API endpoints and then make requests to those endpoints from your app. You’ll need to create an endpoint for each operation you want to perform (eg listing entities from a database).


About endpoint URLs:
If you define an endpoint like this (this example will return JSON data):

@anvil.http.endpoint('/list-people')
def list_people():
  return [{'name': person['name']} for person in app_tables.people.search()]

then you can get its URL by adding /list-people to the URL you see at the bottom of the Server Module editor. Eg if the editor says:

Then the URL to call that list_people() function is:

https://infatuated-sunny-deposit.anvil.app/_/api/list-people

(Note that this example is a public app, so it’s got a nice-looking URL. A private app has a long URL containing a security token, so only people who know the URL can access it. You can make your app public or private by opening the Publish dialog in the Anvil editor.)


If and only if you are writing your mobile app in Python, then it might make sense to use the Anvil Uplink (in Client Code) mode. This lets you call server functions and access data tables just like you do from Anvil Form code. However, you’re probably not using Python (most Android apps are written in Java, and most iPhone apps are written in Objective-C or Swift), so you should probably go with the HTTP Endpoint approach I’ve described above.

3 Likes

Thank you for the example, but I still can’t find the link at the bottom of my Server Module editor!
Can I instead just add :

/_/api/“function name” to my app link to create the HTTP endpoints?

Make sure there are no syntax errors, otherwise the link doesn’t appear. Also, if you are using python 3, make sure you are not using f-strings or anything that is not python 2 correct syntax.

You could try with a new server module containing only your function with the decorator.

1 Like

Here is my app, please review it and let me know what do I need to correct it




Thank you.

Create a new project, add a server module and an endpoint that just has a function with “pass” in it. If that works then something else in you prroject is causing an issue. If not, then …er…dunno :frowning:

Like this - you can see the details down the bottom :

1 Like

Thank you, I can see the link now :slight_smile: , I still have to figure out why I doesn’t appear in my app.

Would you be able to share a clone link to your app, so I could copy it here and see what’s wrong?

1 Like

Sure:
https://anvil.works/build#clone:VULNAX3C5HWXPLMI=EV27UFCWGCH2QUCQMVF77HUR

Ahah!
I see the problem.

@anvil.http.endpoint('/list-people')

is the wrong decorator. Use :

@anvil.server.http_endpoint()

and the relevant detail will appear.

It should appear as an autocomplete as soon as you type the “@” symbol.

3 Likes

You are right! I can see the link now!
Thank you :slight_smile:

1 Like