HTTP API Quick Start
The ExecKernel API runs the engine in a separate service. Your application sends a graph identifier to start a session, then sends the session identifier and a chosen edge to move through it. The API loads the graph, checks the transition and saves the resulting state. Your application uses the response to decide what to show or do next.
In this quick start, you will run the service and learn to start a session, request a transition and retrieve its state through HTTP. You will also be able to use that API from the PHP SDK or the JavaScript and TypeScript SDK.
To practise these operations, we will reuse the example graph built in Your first graph. It represents a choice between store collection and home delivery, with delivery available only for addresses the store serves. Here we will start a session with delivery available and select that option to see what the API returns.
You need Git, Bash, Python 3, curl and Docker with the Compose and Buildx plugins. Docker runs PHP and the API’s dependencies, so you do not need to install PHP on your host for this route. The commands below use a Bash shell and a free local port, 4320. You can use the example’s complete JSON file without completing the local PHP quick start.
Prepare the service and its graph
Section titled “Prepare the service and its graph”Clone the engine and API repositories into the same working directory:
git clone https://github.com/exec-kernel/execkernel.gitgit clone https://github.com/exec-kernel/execkernel-api.gitcd execkernel-api./deploy/build-local.sh ../execkernelThe API repository locks the engine revision it needs. The build helper packages that revision from the engine checkout and builds two local images: the PHP API and its Nginx HTTP entry point. This keeps the API and engine versions together without requiring Composer on your host.
Next, create a directory for the tutorial graph and download the complete example:
mkdir -p tutorial-graphscurl --fail --location --silent --show-error \ https://docs.execkernel.org/examples/getting-started/delivery-options.json \ --output tutorial-graphs/delivery-options.jsonchmod 755 tutorial-graphschmod 644 tutorial-graphs/delivery-options.jsonexport EXECKERNEL_GRAPH_HOST_DIR="$PWD/tutorial-graphs"The API’s graph catalogue is a directory of JSON files mounted into the container. The permissions above let the API’s nonroot user read this catalogue. A request selects the graph by its graph_code, here delivery-options; it does not send a filename or upload a graph. The file also has a numeric graph_id, which the API requires for its catalogue.
Create the service credential and start the API
Section titled “Create the service credential and start the API”The deployment requires a Bearer token. Generate one in the repository’s ignored .secrets directory:
mkdir -p .secretschmod 700 .secretspython3 - <<'PY'from pathlib import Pathimport secrets
token_file = Path(".secrets/api-token")token_file.write_text(secrets.token_hex(32) + "\n")token_file.chmod(0o444)PYThe private directory limits access on the host. Docker mounts the token file into the API container, where the runtime user must be able to read it. This token authorizes access to the service; it is separate from the session identifiers that the API will return. Keep it in your backend or server environment when integrating an application.
In the same shell, start the supplied Compose configuration with its graph catalogue override:
docker compose -p execkernel-tutorial \ -f compose.yaml -f deploy/compose.graphs.yaml \ up -d --no-build --wait
export EXECKERNEL_BASE_URL=http://127.0.0.1:4320export EXECKERNEL_API_TOKEN="$(cat .secrets/api-token)"
curl --fail --silent --show-error "$EXECKERNEL_BASE_URL/healthz"The health response contains "status": "ok" and a request trace identifier. Compose has started the HTTP entry point, loaded the graph catalogue and prepared the SQLite database used to store sessions. The default configuration binds to your own machine’s loopback address, so this is a local service.
If startup fails, inspect the API log with docker compose -p execkernel-tutorial -f compose.yaml -f deploy/compose.graphs.yaml logs api. An unreadable token or graph file prevents startup. If port 4320 is already in use, export another EXECKERNEL_PORT before starting Compose and use that port in EXECKERNEL_BASE_URL.
Start a session and read the available choices
Section titled “Start a session and read the available choices”A session is one execution of a graph. Start one for delivery-options, requesting English text and supplying the fact that home delivery is available:
curl --fail-with-body --silent --show-error \ "$EXECKERNEL_BASE_URL/execkernel/session/start" \ --header "Authorization: Bearer $EXECKERNEL_API_TOKEN" \ --header "Content-Type: application/json" \ --data '{"flow":"delivery-options","lang":"en","context":{"delivery_available":true}}' \ --output session.json
python3 -m json.tool session.jsonflow identifies the graph in the server’s catalogue. lang selects its localized text, and context supplies the application data used by its conditions. In a real checkout, your backend would obtain delivery_available from its delivery coverage check. ExecKernel evaluates the condition against that value; it does not look up the address itself.
The response has three pieces that your application will use:
| Field | What it gives your application |
|---|---|
session_id |
An identifier to retain with the order and send in later requests for this execution. |
state |
The current node, context and execution history. At this point, current_node is 1. |
payload |
The content for the current step: its title and the outgoing choices, including their availability. |
The response also includes a trace_id for diagnosing that HTTP request. It is not the session identifier.
Here is an excerpt of payload, keeping only the fields needed to present the choices:
{ "type": "node", "node_id": 1, "title": "How would you like to receive your order?", "edges": [ { "id": 10, "labels": [{"text": "Collect from store"}], "availability": "available" }, { "id": 20, "labels": [{"text": "Home delivery"}], "availability": "available" } ]}Your application can use the title as a heading and the edge labels as buttons or menu options. Retain each edge’s id with its control: the next request uses that identifier to express the user’s choice. In this example both choices are available. Starting another session with delivery_available: false would leave collection available and mark home delivery as blocked.
Choose home delivery
Section titled “Choose home delivery”Extract the identifier of the session you just created so the remaining commands can refer to it:
SESSION_ID="$(python3 -c 'import json; print(json.load(open("session.json"))["session_id"])')"Now send edge 20, the home delivery choice:
curl --fail-with-body --silent --show-error \ "$EXECKERNEL_BASE_URL/execkernel/session/next" \ --header "Authorization: Bearer $EXECKERNEL_API_TOKEN" \ --header "Content-Type: application/json" \ --data "{\"session_id\":\"$SESSION_ID\",\"edge_id\":20}" \ --output next.json
python3 -m json.tool next.jsonThe API retrieves the saved session and checks that edge 20 can be taken from its current node with its context. Because the condition passes, it stores the new state at node 3. You send the choice, not a replacement state or a destination node.
The new payload is terminal: this delivery-selection process has reached an end. The relevant excerpt is:
{ "type": "terminal", "node_id": 3, "title": "Home delivery selected", "outputs": [ { "type": "content", "lang": "en", "content": "Confirm the delivery address for your order." } ]}Your checkout can now display that message and continue its own address-confirmation process. Reaching this terminal node does not create a shipment or update your order database. Those actions belong to your application.
Retrieve the session later
Section titled “Retrieve the session later”A later HTTP request can retrieve the same execution by its identifier. It does not need to resubmit the graph or the previous response:
curl --fail-with-body --silent --show-error \ "$EXECKERNEL_BASE_URL/execkernel/session/resume" \ --header "Authorization: Bearer $EXECKERNEL_API_TOKEN" \ --header "Content-Type: application/json" \ --data "{\"session_id\":\"$SESSION_ID\"}" \ --output resumed.json
python3 -m json.tool resumed.jsonThe returned state still has current_node: 3, and the payload describes the selected home delivery result. resume reads the saved position; it does not take another edge. The supplied deployment keeps sessions in a Docker volume, with a default lifetime of 24 hours. An expired or deleted session cannot be resumed.
The Bearer token and the session identifier serve different purposes: the token admits the caller to this deployment, while the identifier selects an execution. In your backend, associate the execution with the appropriate order or user and check who may use it.
If a request returns 401, check that your shell’s token matches .secrets/api-token. If starting the session cannot find delivery-options, check that its JSON file is in the mounted catalogue and that the request uses its graph_code. If you choose a blocked edge, the API rejects the transition; the application should keep the current step and let the user choose an available option.
Continue with a client
Section titled “Continue with a client”Keep the service running for the Remote PHP Quick Start or the JavaScript & TypeScript Quick Start. Those clients perform the HTTP exchange for you and expose the same session operations.
For a more complete application, explore Technical Support in the Showcase. Follow a branch, then open Technical inspector to compare the current node and execution history with the screen shown by the application. The Showcase runs its own demo service; it does not connect to your local instance.
When you have finished with the local service, stop it from the API repository. In a new shell, first restore the catalogue path with export EXECKERNEL_GRAPH_HOST_DIR="$PWD/tutorial-graphs":
docker compose -p execkernel-tutorial \ -f compose.yaml -f deploy/compose.graphs.yaml downThis removes the tutorial containers and networks while retaining the session volume. Use the same Compose files and project name when starting it again.