Python FastAPI

Python FastAPI Quickstart

You can use any language or framework in a session backend. If you can run it in a Docker container, you can run it on Jamsocket. This quickstart will cover the essential setup for running a Python FastAPI backend.

💡

Note: we'll be using Docker's command-line tool (opens in a new tab) so make sure Docker is installed before starting.

Write your session backend code and Dockerfile

Let's create the following files for our session backend in a directory called app.

app.py
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
 
app = FastAPI()
 
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
)
 
@app.get("/")
async def read_root():
    return {"res": "Hello World"}
requirements.txt
fastapi==0.108.0
uvicorn==0.25.0

To package your session backend into a container, write a Dockerfile that Jamsocket can use to build and push your session backend code.

Dockerfile
FROM python:3.9.6-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]

Developing your code locally

Run the Dev CLI

npx jamsocket dev --dockerfile ./Dockerfile --watch .

npx jamsocket dev will start a Jamsocket dev server at localhost:8080. Check out the Dev CLI for more details on how the Dev CLI works.

Connect to a session backend

Get a Connection URL

To connect to your session backend, use the Jamsocket HTTP API or client libraries to get a connection URL.

Try making a curl request to the connect endpoint from the HTTP API:

curl -X POST http://localhost:8080/v2/service/MY_ACCOUNT/MY_SERVICE/connect

A successful JSON response will include a connection url. Use this URL to access your session backend API.

connect-result-example
{
  "backend_id": "ebb8ljmndix2ge",
  "spawned": true,
  "status": "scheduled"
  "token": "ClpYXET8OA-ie7ufWFHE5y0Q-NbghE04niQ2fwBZe40",
  "url": "http://localhost:9090/ClpYXET8OA-ie7ufWFHE5y0Q-NbghE04niQ2fwBZe40/",
  "secret_token": "129aky7mT0ym-x0w5I88eTn41FFJawYwV4EgabC4r12",
  "status_url": "http://localhost:8080/v2/backend/ebb8ljmndix2ge/status",
  "ready_url": ""
}
💡

In development, you don't need to supply the endpoints with real account or service information. The next step, deploying to production, will cover account and service creation.

Connect to your session backend API

When the backend returned from the connect endpoint shows a Ready status, make a curl request to an endpoint in your session backend api using the connection url.

A GET request to / should return the Hello World string from the backend.

curl CONNECTION_URL_FROM_SPAWN_REQUEST

Deploy to Jamsocket

Login to Jamsocket

npx jamsocket login

Create a service

npx jamsocket service create my-hello-world

You should see the service appear on your Jamsocket dashboard (opens in a new tab).

Build and push your session backend code to Jamsocket

You can build your session backend docker image and push to Jamsocket's container registry with a single command:

npx jamsocket push my-hello-world --dockerfile ./Dockerfile

Connect to session backends in production

To use the HTTP API in production, create an API token from the Jamsocket dashboard (opens in a new tab). You'll also need to provide your account and service name in the connect endpoint.

A connect request should look like this:

curl \
  -X POST \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{}" \
  https://api.jamsocket.com/v2/service/ACCOUNT_NAME/my-hello-world/connect

Just like in development, making a request to the connection url should return a the hello world string from the backend.

curl CONNECTION_URL_FROM_SPAWN_REQUEST

See your session backend's logs

Finally, you can view logs from your session backends in Production with the Jamsocket CLI.

npx jamsocket logs BACKEND_NAME

Or view logs from your dashboard (opens in a new tab) when you click on the backend name in the Service page.

Next Steps

Built by Jamsocket.