Skip to main content

Run a Function in the Cloud

The simplest way to run code on Beam is to add the @function decorator to any Python function. Save this to app.py:
app.py
from beam import function


@function(cpu=1, memory="1Gi")
def square(x: int):
    return {"result": x**2}


if __name__ == "__main__":
    print(square.remote(x=12))
Run it like any other Python file:
python app.py
Beam syncs your code, launches a container, runs the function, and streams the result back to your shell:
=> Building image
=> Using cached image
=> Syncing files
=> Files synced
=> Running function: <app:square>
{'result': 144}
=> Function complete
The container spins up in seconds, runs your code, and shuts itself down. No idle costs, no infrastructure to clean up.

Deploy a Web Endpoint

To turn your code into a live web API, swap @function for @endpoint. We’ll include numpy in the image to show how easily you can add Python packages.
  • Image() defines your container environment. You can add Python packages, system dependencies, or even custom Dockerfiles.
  • @endpoint turns your function into a real, live web API that runs in the cloud.
app.py
from beam import endpoint, Image


@endpoint(
    name="quickstart",
    cpu=1,
    memory="1Gi",
    image=Image().add_python_packages(["numpy"]),
)
def predict(**inputs):
    x = inputs.get("x", 256)
    return {"result": x**2}

Deployment

Deploy the endpoint to the cloud:
beam deploy app.py:predict

Call the API

When the deploy finishes, Beam prints your endpoint URL along with a ready-to-run curl command. Replace [TOKEN] with your token and use the URL from your deploy output:
curl -X POST 'https://app.beam.cloud/endpoint/quickstart' \
  -H 'Authorization: Bearer [TOKEN]' \
  -H 'Content-Type: application/json' \
  -d '{"x": 12}'
Either way, you’ll get back:
{ "result": 144 }
The container spins up in seconds, runs your code, and shuts itself down. No idle costs. No infrastructure to clean up.

What Next?

Here are some other things you can try: