What’s the simplest, preferably cheap way to put a simple (python) API online?
I’ve done a few over the years. One was a kind of personal quote server, returning a plain-text quote from a file I’ve collected over time. (Terpstra-like, I’ve expanded it. Now the web version is prettier and it can also supply json to feed a widget on my phone and more.)
Heroku works. But I have to figure it out each time, and it’s gotten pricier.
Is there an easier approach? I’ve seen people post about using GitHub actions, but I haven’t found a tutorial detailed enough to really understand what I’d be doing.
I think you could get a $5/month VPS for this task. It might be overkill, but you don’t have to fuss about making sure that a computer in your home is running all the time and has an accessible IP address. You don’t have to wait for your app to spin up, either, which is the downside to some of these serverless options.
A DigitalOcean droplet is like $5/month. Vultr has a $2.50/mo plan for 1 vCPU and 512mb RAM. You select a distro, configure the firewall to expose the ports you want accessible by your API, then just run the app. If you use something like
tmux that would make it dead simple to keep the process running for as long as you need it to. I have several processes running just like that on my VPS and it’s been completely reliable for my use.
If you go about it this way, adding another API would just be a matter of opening up another port and running the application on that port.
If you want to point it to a specific route, you can set
nginx up to serve a reverse proxy, then you can have the APIs point to a specific route or subdomain.
Yeah, Digital Ocean Droplets are ideal if you don’t have your own VPS somewhere.
Checkout DO’s Functions (similar to AWS Lambda). My son-in-university setup a weekly report that uses python to download a web page, parse it, then send an email out… there’s no storage, logs get sent to a free log ingesting service (so we know if it works, or breaks) and email uses a regular gmail account. It’s very cheap to run. Github actions is used to deploy the code to DO as well as to run the function on demand or scheduled.
Happy to answer questions… or pass them on if they make no sense to me! LOL
I’ve dabbled with functions/serverless/edge stuff. If you’ve just got one simple script to run, maybe this approach is best.
DO’s App Platform is also really nice middle ground. More capable than functions but still completely managed externally, so you only have to worry about your code.
But I’ve come to believe that the best approach for me is to use a VPS. These are pretty cheap you can get one for like $5 a month. And you can run literally any programming language, server, or software on them. The only downside is it does require a little bit of learning how to administer a Linux system.
I say downside, but it’s actually really FUN learning about all this stuff. It’s just there’s a lot to learn and it can be a little overwhelming.
But having a computer in the cloud that you can log into, do anything you want with, it’s a great resource to have at hand.
However as long as you back it up and don’t put any sensitive data on there, you should be fine
I agree with Brett that DigitalOcean droplets are a great way to do VPS. Their marketplace has a range of preconfigured servers for various applications. But then you can continue to evolve and adapt your server as your needs change.
DO Is also a lot more user-friendly than things like Azure, Google Cloud or AWS. (The conplexity of the control panels on those things, my gosh!)
For me, their nginx base server has been a great starting point for a web server.
Don’t even get me started on Oracle’s control panels.
Thanks. This is all very helpful.
I am torn between a VPS – for o e thing, I could probably run several APIs and lightweight apps from one, instead of multiple Heroku projects that each add to the cost. But … I’m not sure I want to run a web server. I guess that’s what I have to come to terms with …
If you want to do the webserver route, I recommend using the Nginx pre-configured droplet and use Nginx as the “front” to the world. This droplet has only ports 22 (SSH), 80 (HTTP) and 443 (HTTPS) enabled and everything else is firewalled.
You can then set up nginx to run one or more “reverse proxy” configs, each of which funnels traffic for a particular hostname / path to a process running on the machine on its own port.
So e.g. api.example.com:443 has nginx terminate the SSL, and forwards to localhost:3000 where your python app is running.
Then you can have another nginx config my-other-app.some-other-domain.com:443 to localhost:3100 where some other app is running.
It’s quite a neat setup and very flexible. You can serve different sub-paths from different places, including to external servers, serve static files for some routes, etc. You have full control.
You can also register SSL certs at the commandline via LetsEncrypt using
certbot tool (preinstalled on the droplet) which is dead easy to use and integrates with nginx. It’s all really quite nice.
Assuming this isn’t scaring you off to Heroku you’ll have a fun time
This is super helpful. Thanks! I have all sorts of little utilities I want to make available for my own use, but from iOS as well as Mac, and any machine… this sounds doable. Or at least learnable – I can probably start figuring it out from here. Thank you!
Wow. This is going great so far. Thanks again to @nick @least @ttscoff and @theconsultant for tips and advice, all good (though I haven’t tried DO Functions yet)