The easiest way to deploy your Python API for free — using Deta Cloud (and not Heroku).
TLDR: Ditch Heroku. Deta Cloud is the easiest way to deploy python code to the cloud.
Heroku (vis-à-vis Salesforce) has betrayed developers. This goes beyond their recent announcement of eliminating their free plans (which they’ve astonishingly blamed on developers), beyond their unspoken feature freeze, and perhaps beyond the devastating security breach they’ve experienced and resisted telling the public about until outsiders sounded the alarm (to this day, they have not disclosed the full extent of the breach — I’m hoping the AG’s office brings them to court so we can see the damage and hold them accountable). Heroku’s / Salesforce’s betrayal comes from deprioritizing developers who use their product, disempowering developers inside their own organization, and for neglecting the trust developers placed in them.
Alternatives to Heroku
Needless to say, I’ve been looking for a different platform to deploy my API’s. There are many Heroku alternatives and I’m honestly feeling overwhelmed by the thought of evaluating all of them. However, I did find one Heroku alternative which I did like.
Read through the thread to see the numerous alternatives to Heroku.
Have a Heroku alternative you think is really cool? Leave a comment below and I’ll do my best to try it out (and maybe write about it). My time is limited, so I will only be able to get to a few.
Deta describes itself as “the universe’s most developer friendly cloud platform.” Deta is also free — according to their site, “forever.”
https://www.deta.sh/
Let’s see if their platform holds up to their testament.
Let’s create a simple API using FastAPI (python’s most popular web development library), which we’ll be deploying to deta.
1.1 Create a main.py file with your FastAPI code:
⚠️ NOTE: The entry point to your API must be a file named
main.py
Sample API code (I recommend you use this for your first iteration):
Learn more about FastAPI here.
1.2 Configure dependencies in a requirements file
:
Create a requirements.txt
file and add fastapi
as a requirement.
Bash command to do this is below:
NOTE: Feel free to do with a visual editor too, whichever you prefer.
echo "fastapi" >> requirements.txt
1.3 Store secrets:
Create a .env
file to store your secret value:
mkdir secret
echo "SECRET_VALUE=pickles" >> secret/.env
⚠️ NOTE: If you are tracking your code in github, make sure you are ignoring the contents of secret/
in your .gitignore
file. Here is the .gitignore
file I used for my code.
Let’s make sure it’s working locally before deploying anything.
2.1 Install requirements
- Make sure you have
fastapi
installed. - Also make sure you have
uvicorn
installed. (Don’t include as a requirement though, since this is only used when running locally.) - You can install both of these by running
pip install fastapi uvicorn
(If this doesn’t work, try switchingpip
withpip3
)
2.2 Load your secret value:
source secret/.env
Alternatively: export SECRET_VALUE=not-pickles
2.2 Launch FastAPI local server:
uvicorn main:app --reload --port 8000
2.3 Test out local API endpoints:
- Navigate to
localhost:8000
— you should hit your api’s endpoint! - Navigate to
localhost:8000/secret
— you should hit your api’s secret endpoint! 🎉 - Bonus: View the richly formatted swagger UI by navigating to
localhost:8000/docs
If you’re on a mac, execute the following:
curl -fsSL https://get.deta.dev/cli.sh | sh
If not on a mac, follow the documentation guide for correct installation command.
3.2 Verify the CLI is installed:
Open a new terminal window (if you re-use the same terminal session, things may not work for you).
Execute deta --help
If you get a response, you are done! 🎉
If you get the error: zsh: command not found: deta
that means your terminal cannot find the executable program. Make sure that deta
was installed correctly. Try exiting terminal and opening a new window. (Still have issues? Join their Discord and folks will help you.)
3.3 Login to Deta (and sign up for an account)
For housekeeping sake, make sure you are in the same directory as your code. Login with:
deta login
Here’s what happens next:
- Your default browser will open up to deta’s login page.
- You will be prompted to sign up for an account.
- Following that, you will need to confirm your email address.
- Then you sign in again (make sure it’s the same link as the url in your terminal)
If you get the message Logged in successfully.
then you are done! 🎉
3.4 Create a new deta project
This is done through the UI. (I don’t see any support for CLI configuration — maybe they’ll build that soon.)
- If your browser is closed, log back into Deta’s web interface: https://web.deta.sh/home
- Click
Create First Project
and choose a location near-ish to you.
3.5 Create a new “micro” (Deta’s name for an app):
# Choose a name for your micro
export MICRO_NAME=getting-started-with-deta# This command does the thing
deta new --name $MICRO_NAME --python
Verify that this worked by executing the following: cat .deta/prog_info
If stuff is printed to the console, then this file exists and you are good.
3.6 Add your environment variables to Deta
This syncs your secrets to the cloud. Documentation link: Deta Setting Environment Variables
deta update -e secret/.env
3.7 Enable Logging
Also possible through the UI. Documentation Link: Visor (Logs)
deta visor enable
3.8 Deploy! ⭐️
deta deploy
Note: Once you are authenticated and your project is configured, you can deploy changes to your API with the above command. (It’s that simple.)
Navigate to your project in Deta Cloud and find the URL specific to your micro.
Alternatively, list information about your micro by executing:
deta details
If you have jq
installed, you can get the url by executing:
deta details | jq -r '.endpoint'
😎 Want to do something cool? Open your browser directly from terminal:
deta details | jq -r '.endpoint' | xargs open
4.1 Test out your endpoints:
(I’ll be posting the links to mine, so you can see what to expect)
We’re done! 🎉