Beam allows you to deploy web endpoints that can be invoked via HTTP requests. These endpoints can be used to run arbitrary code. For instance, you could perform inference using one of our GPUs, or just run a simple function that multiplies two numbers.

from beam import endpoint

@endpoint(
    cpu=1.0,
    memory=128,
)
def multiply(**inputs):
    result = inputs["x"] * 2
    return {"result": result}

Launch a Preview Environment

beam serve monitors changes in your local file system, live-reloads the remote environment as you work, and forwards remote container logs to your local shell.

Serve is great for prototyping. You can develop in a containerized cloud environment in real-time, with adjustable CPU, memory, GPU resources.

It’s also great for testing an app before deploying it. Served functions are orchestrated identically to deployments, which means you can test your Beam workflow end-to-end before deploying.

To start an ephemeral serve session, you’ll use the serve command:

beam serve app.py:func
Sessions end automatically after 10 minutes of inactivity.

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.

Deploying the Endpoint

To deploy the endpoint, enter your shell and run this command from the working directory:

beam deploy [FILE.PY]:[ENTRY-POINT] --name [NAME]

After running this command, you’ll see some logs in the console that show the progress of your deployment.

Calling the Endpoint

After deploying the API, you’ll be able to copy a cURL request to hit the API. Open another terminal window to invoke the API:

Example Request

curl -X POST 'https://app.beam.cloud/endpoint/multiply-numbers/v1' \
-H 'Accept: */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Connection: keep-alive' \
-H 'Authorization: Bearer [YOUR_AUTH_TOKEN]' \
-H 'Content-Type: application/json' \
-d '{"x": 10}'

Example Response

{
  "result": {
    "result": 20
  },
  "msg": "",
  "error_msg": ""
}

Context

Context is a special kind of endpoint input. It contains a variety of values that are useful for debugging. For instance, it gives you access to the container ID, stub ID, task ID, and more. For more information on context you can visit its sdk section.

In addition, any return values from the on_start function is stored in the context in the on_start_value field.