The scripts p1-inputs.sh, p1-inputs.py and p1-inputs.ts are in full under Complete script.
Before you start
Set CONTEXT_BASE_URL, CONTEXT_API_KEY and AGENT_ID as in the Quickstart.
Client setup used by every step
#!/usr/bin/env bashset -euo pipefail: "${CONTEXT_BASE_URL:?Set CONTEXT_BASE_URL, for example https://api.context.ai}": "${CONTEXT_API_KEY:?Set CONTEXT_API_KEY to a personal Task API key}": "${AGENT_ID:?Set AGENT_ID to an id from GET /v1/agents}"api() { local path=$1; shift curl -sS --fail-with-body "$CONTEXT_BASE_URL/v1$path" -H "x-api-key: $CONTEXT_API_KEY" "$@"}fail() { echo "error: $*" >&2; exit 1; }# Opens the session's event stream; exec makes the background job's pid the curl process itself.stream() { exec curl -sS --fail-with-body -N "$CONTEXT_BASE_URL/v1/sessions/$1/events/stream" \ -H "x-api-key: $CONTEXT_API_KEY" "${@:2}"}# The JSON of every event received so far on a stream saved to a file. Ping frames carry no id and are skipped.events_in() { sed -n 's/^data: //p' "$1" | jq -c 'select(.id)'; }# Waits until the stream file holds a session.status_idle that ends the turn (not requires_action).wait_for_end_of_turn() { for _ in $(seq 1 300); do grep -q '^event: error' "$1" && fail "the stream ended with an error: $(grep -A2 '^event: error' "$1" | sed -n 's/^data: //p')" events_in "$1" | jq -se 'any(.[]; .type == "session.status_idle" and .stop_reason.type != "requires_action")' >/dev/null && return 0 sleep 1 done fail "the turn did not end within 300 seconds"}WORK=$(mktemp -d)
Upload a file
Send a multipart request with one part named file that carries a filename. The file is stored in your Drive, your file storage in the Context app, and the response is its metadata.
step 2: session dd13d728-93e3-440a-874c-4efeb58c97e0 has 329409ba-f288-4b44-9c73-14a18150edee at inputs/cities.csv
Ask the agent about the file
Open the stream first, then send the message. The turn is over at a session.status_idle whose stop_reason.type is not requires_action.
stream "$SESSION_ID" > "$WORK/stream.txt" &STREAM_PID=$!sleep 1 # open the stream before sendingkill -0 "$STREAM_PID" 2>/dev/null || fail "the stream did not open: $(cat "$WORK/stream.txt")"api "/sessions/$SESSION_ID/events" -H "content-type: application/json" -d '{"events": [{"type": "user.message", "content": [{"type": "text", "text": "Read inputs/cities.csv and name the largest city."}]}]}' > /dev/nullwait_for_end_of_turn "$WORK/stream.txt"kill "$STREAM_PID"ANSWER=$(events_in "$WORK/stream.txt" | jq -r 'select(.type == "agent.message") | .content[] | select(.type == "text") | .text')[[ -n $ANSWER ]] || fail "no agent.message on the stream"echo "step 3: agent said: $ANSWER"
Sample output:
Code
step 3: agent said: Tokyo is the largest city in the file, with 37.4 million people.
Archive the session and delete the file
Archiving ends the session. Its status becomes terminated and its history stays readable. A file that a session still uses cannot be deleted until that session is archived.
STATUS=$(api "/sessions/$SESSION_ID/archive" -X POST | jq -r .status)[[ $STATUS == "terminated" ]] || fail "expected terminated, got $STATUS"# Allowed once no live session uses the file.DELETED=$(api "/files/$FILE_ID" -X DELETE)[[ $(jq -r .type <<<"$DELETED") == "file_deleted" ]] || fail "unexpected delete response: $DELETED"echo "step 4: session $STATUS, file $FILE_ID deleted"
The multipart part is not named file, has no filename, or there is more than one part.
Send exactly one part named file with a filename: a (name, bytes, type) tuple in Python, toFile(bytes, name) in TypeScript, or -F file=@path with cURL.
403 permission_error on sessions.create
AGENT_ID is not an agent this key can run. An id that does not exist returns the same 403.
Pick an id from GET /v1/agents.
404 not_found_error naming the file on sessions.create
The file id is not one uploaded through /v1/files that this key can read.
Use the id returned by the upload in step 1.
400 invalid_request_error on the file delete
A session that is not archived still uses the file, or an archived session whose last turn is still running uses it.
Archive that session, then delete the file.
Complete script
#!/usr/bin/env bashset -euo pipefail: "${CONTEXT_BASE_URL:?Set CONTEXT_BASE_URL, for example https://api.context.ai}": "${CONTEXT_API_KEY:?Set CONTEXT_API_KEY to a personal Task API key}": "${AGENT_ID:?Set AGENT_ID to an id from GET /v1/agents}"api() { local path=$1; shift curl -sS --fail-with-body "$CONTEXT_BASE_URL/v1$path" -H "x-api-key: $CONTEXT_API_KEY" "$@"}fail() { echo "error: $*" >&2; exit 1; }# Opens the session's event stream; exec makes the background job's pid the curl process itself.stream() { exec curl -sS --fail-with-body -N "$CONTEXT_BASE_URL/v1/sessions/$1/events/stream" \ -H "x-api-key: $CONTEXT_API_KEY" "${@:2}"}# The JSON of every event received so far on a stream saved to a file. Ping frames carry no id and are skipped.events_in() { sed -n 's/^data: //p' "$1" | jq -c 'select(.id)'; }# Waits until the stream file holds a session.status_idle that ends the turn (not requires_action).wait_for_end_of_turn() { for _ in $(seq 1 300); do grep -q '^event: error' "$1" && fail "the stream ended with an error: $(grep -A2 '^event: error' "$1" | sed -n 's/^data: //p')" events_in "$1" | jq -se 'any(.[]; .type == "session.status_idle" and .stop_reason.type != "requires_action")' >/dev/null && return 0 sleep 1 done fail "the turn did not end within 300 seconds"}WORK=$(mktemp -d)# @step 1 Upload a fileprintf 'city,population_millions\nTokyo,37.4\nDelhi,32.9\nShanghai,29.2\n' > "$WORK/cities.csv"FILE=$(api /files -F "file=@$WORK/cities.csv;type=text/csv")FILE_ID=$(jq -r .id <<<"$FILE")[[ $(jq -r .filename <<<"$FILE") == "cities.csv" ]] || fail "unexpected upload: $FILE"echo "step 1: uploaded $FILE_ID cities.csv ($(jq -r .size_bytes <<<"$FILE") bytes)"# @step 2 Create a session with the file attachedSESSION=$(api /sessions -H "content-type: application/json" -d "$(jq -n --arg agent "$AGENT_ID" --arg file "$FILE_ID" '{ agent: $agent, environment_id: "env_default", title: "Cities report", resources: [{type: "file", file_id: $file}]}')")SESSION_ID=$(jq -r .id <<<"$SESSION")[[ $(jq -r '.resources[0].file_id' <<<"$SESSION") == "$FILE_ID" ]] || fail "file is not attached: $SESSION"MOUNT=$(jq -r '.resources[0].mount_path' <<<"$SESSION")[[ $MOUNT == "inputs/cities.csv" ]] || fail "unexpected mount_path: $MOUNT"echo "step 2: session $SESSION_ID has $FILE_ID at $MOUNT"# @step 3 Ask the agent about the filestream "$SESSION_ID" > "$WORK/stream.txt" &STREAM_PID=$!sleep 1 # open the stream before sendingkill -0 "$STREAM_PID" 2>/dev/null || fail "the stream did not open: $(cat "$WORK/stream.txt")"api "/sessions/$SESSION_ID/events" -H "content-type: application/json" -d '{"events": [{"type": "user.message", "content": [{"type": "text", "text": "Read inputs/cities.csv and name the largest city."}]}]}' > /dev/nullwait_for_end_of_turn "$WORK/stream.txt"kill "$STREAM_PID"ANSWER=$(events_in "$WORK/stream.txt" | jq -r 'select(.type == "agent.message") | .content[] | select(.type == "text") | .text')[[ -n $ANSWER ]] || fail "no agent.message on the stream"echo "step 3: agent said: $ANSWER"# @step 4 Archive the session and delete the fileSTATUS=$(api "/sessions/$SESSION_ID/archive" -X POST | jq -r .status)[[ $STATUS == "terminated" ]] || fail "expected terminated, got $STATUS"# Allowed once no live session uses the file.DELETED=$(api "/files/$FILE_ID" -X DELETE)[[ $(jq -r .type <<<"$DELETED") == "file_deleted" ]] || fail "unexpected delete response: $DELETED"echo "step 4: session $STATUS, file $FILE_ID deleted"# @end