- Building apps using FastAPI
- Using the hot-reloading feature to develop locally
- Deploying apps to a persistent REST endpoint
Import Beam modules
You’ll start by importing a BeamApp
and Runtime
App
is the namespace for a project. You’ll give it a unique name as an identifier.- Inside the
App
is aRuntime
. TheRuntime
is a definition of the hardware your container will run on.
app.py
This app requires
pydantic
to be installed on your local machine. You can install it by running pip install pydantic
.Running a function to retrieve Reddit posts
We’re going to deploy a function that retrieves top posts from a given subreddit.app.py
Developing your app on Beam
Beam includes a live-reloading feature that allows you to run your code on the same environment you’ll be running in production.By default, Beam will sync all the files in your working directory to the remote container. This allows you to use the files you have locally while developing. If you want to prevent some files from getting uploaded, you can create a
.beamignore
.beam serve app.py
. This will:
- Spin up a container
- Run the container on a cloud machine
- Print a cURL request to invoke the API
- Stream the logs to your shell
beam serve
, you’ll notice the server reloading with your code changes.
You’ll use this workflow anytime you’re developing an app on Beam. Trust us — it makes the development process uniquely fast and painless.
Call the API
Open a browser window and paste the URL printed when you ranbeam serve
.
You can customize this URL with the name of a subreddit. In this example, we’ll use r/machinelearning
.
GET
request returns a top post from the subreddit:
Deployment
Now it’s time to deploy the app to create a persistent endpoint. In your shell, run this command to deploy your app:beam serve
session, this will create a persistent deployment and will remain active until you stop it.