Separate out code from main app to be used as API call from third party interface

What I’m trying to do:
I have some code that currently sits in my own server and my app uses uplink to connect to it. I need to host this as a separate package of code that can be called by an API call by an external third part using their own front end interface.

Should I move this to an Anvil server module (either as a new app or within my current app but as a separate server module?) and package this as a separate API (using the API decorator function in docs) that will be called by the clients?

Looking at some forum posts and documentation, I think the answer is yes, we can - so is there a specific starting point where I can get started please?

I also want to understand what the charges might be per API call etc. and if there are limits to amount of data that can be passed through.

Also which plan is best suited for purposes like this?

What I’ve tried and what’s not working:
I’m not sure where to start, to be honest. Any pointers will be helpful.

Code Sample:

# code snippet

Clone link:
share a copy of your app

The docs on HTTP endpoints is the place to start: Anvil Docs | Creating HTTP APIs

Put together a few simple APIs and see how they behave when called externally. Rest Test is a good site for calling them: https://resttesttest.com/

Chances are you’ll run into CORS issues, the docs have a section on that. If you want to pass JSON to the APIs, there’s more hoops to be jumped through, but start small and get that working before you move to the more complicated bits.

The most important thing to remember is that any errors in your code (syntax errors, etc) will come back to an external site as an HTTP error. I’ve spent a lot of embarrassing time chasing down spurious errors that were due to syntax issues in recent code changes.

I can’t comment on the charge/usage aspects.

OK great, thanks for this. So in essence, of course, I should try and set up a mock as another app and test all these bits, then mimic on the real app. I already have no idea what CORS issues are, but thank you for pointing out these out, will keep an eye and read up. :slight_smile: Thank you so much!

That’s the approach I’d recommend. Have fun!

Hi @i.rajeshwari! It sounds like you’re getting good advice, but I wanted to make sure you knew that the @anvil.server.http_endpoint decorator works on the uplink as well as in server modules! So the question of how to expose something as an HTTP API is entirely independent of whether to run it in the Uplink or in a Server Module.

2 Likes

So do you mean just use client uplink? If so, why would I then need the decorator? Also, if I set it up as client uplink, can my own anvil app continue to speak to it too?

Sorry, I am still trying to get my head around all this.

There are two concepts overlapping here: one is “how do I expose this service to my customers?”, and the other is “where do I run my server code?”

1. How do I expose my API?
It is possible to expose your service either as a traditional HTTP API (using @anvil.server.http_endpoint) or over the Client uplink (whereby your customers would receive a Client Uplink key, then call anvil.server.connect(), then access your server functions with anvil.server.call()).

If you’re not sure which to choose, I would recommend a traditional HTTP API. It’s a bit more awkward to use than the Uplink, and you do have to mash everything into JSON, but your customers are more likely to be familiar with it.

Of course, both the @anvil.server.http_endpoint function and the @anvil.server.callable function can share most of the same implementation (they can both call the same underlying functions to do the work).


2. Where do I run my server code?
This question is entirely independent of question 1!

You currently have code on your own server, connected to your Anvil app via the Uplink. You are writing code on your server, and you expose some functions with @anvil.server.callable, which makes them callable from the client. If a customer connects via the Client Uplink, they can still anvil.server.call() your function, even though it’s also on the Uplink!

Likewise, if you write a function on your server and decorate it with @anvil.server.http_endpoint, you will be able to call it by visiting https://your-app.anvil.app/_/api/your-endpoint-url, even though it’s running on the Uplink.

Now, you may also choose to move some of your code into a Server Module, in order to run it on Anvil’s servers rather than your own. But you don’t need to do this in order to expose a public API.


The rule of thumb is: anything you can do from a Server Module, you can do from the Uplink. This includes defining API endpoints that are accessible from the wider Internet!

3 Likes

Thank you so much. Very clear now. Will update when I have this working.