Skip to content
Context docs
Build

Quickstart

Before you begin

  1. Make sure your organization has at least one agent that you can run. If it has none, create one in the app or with Create an agent.
  2. Install the tools for the language you use: curl and jq, the anthropic Python package, or the @anthropic-ai/sdk Node package.
If you know Anthropic’s quickstart, the main differences are that you pick an existing agent and don’t create an environment. Step 2 lists your agents, and sessions use env_default. For every other difference, see Coming from Anthropic’s API.
  1. Create a key and set your host

    Create a personal task API key with the default capabilities, and copy it. If you aren’t an organization administrator, first clear the two capabilities that only an administrator can grant, as step 4 of Create a key says. If an automation calls your agents instead of you, see Choose a key. In every shell you use, export CONTEXT_BASE_URL and CONTEXT_API_KEY as shown in the cURL tab. The Python and TypeScript clients read both from the environment. Set CONTEXT_BASE_URL to the host without /v1, because the SDKs add /v1 themselves.

    export CONTEXT_BASE_URL="https://api.context.ai" export CONTEXT_API_KEY="tsk_..."
  2. Pick an agent

    List the agents your key can run. In the last line, replace the example ID with the ID of the agent you want.

    curl -sS "$CONTEXT_BASE_URL/v1/agents" -H "x-api-key: $CONTEXT_API_KEY" | jq -r '.data[] | "\(.id) \(.name)"' export AGENT_ID="6f1c2a9e-0d4b-4c57-9a3e-2b7d8e1f4c60"
  3. Upload a file

    Make a small CSV and upload it.

    printf 'city,population\nTokyo,37400000\nDelhi,31000000\n' > cities.csv export FILE_ID=$(curl -sS -X POST "$CONTEXT_BASE_URL/v1/files" -H "x-api-key: $CONTEXT_API_KEY" \ -F "file=@cities.csv;type=text/csv" | jq -r .id)
  4. Create a session with the file

    Create a session that runs the agent with the file attached, in the env_default environment. The response lists the file under resources with the mount_path where the session finds it.

    export SESSION_ID=$(curl -sS -X POST "$CONTEXT_BASE_URL/v1/sessions" -H "x-api-key: $CONTEXT_API_KEY" \ -H "content-type: application/json" \ -d "{\"agent\": \"$AGENT_ID\", \"environment_id\": \"env_default\", \"title\": \"Quickstart\", \"resources\": [{\"type\": \"file\", \"file_id\": \"$FILE_ID\"}]}" | jq -r .id)
  5. Open the stream, then send a message

    Open the stream, then send a message. With cURL, run the stream in one terminal and send the message from a second terminal that has the same variables set. The loop stops at the first session.status_idle event whose stop_reason.type isn’t requires_action. If the agent asks to run a tool that needs your approval, follow the tool approvals playbook.

    # Terminal 1: watch the stream curl -sSN "$CONTEXT_BASE_URL/v1/sessions/$SESSION_ID/events/stream" -H "x-api-key: $CONTEXT_API_KEY" # Terminal 2: send the message curl -sS -X POST "$CONTEXT_BASE_URL/v1/sessions/$SESSION_ID/events" -H "x-api-key: $CONTEXT_API_KEY" \ -H "content-type: application/json" \ -d '{"events": [{"type": "user.message", "content": [{"type": "text", "text": "Read inputs/cities.csv and name the largest city."}]}]}'

    The agent replies with something like Tokyo is the largest city, with 37,400,000 people. With cURL, terminal 1 prints one frame per event. The reply arrives as an agent.message frame, and a session.status_idle frame marks the end of the reply. Each frame has id, event and data lines, as shown in Stream events.

  6. Archive the session and delete the file

    Archive the session, then delete the file. Archive first, because a file that a session still uses can’t be deleted.

    curl -sS -X POST "$CONTEXT_BASE_URL/v1/sessions/$SESSION_ID/archive" -H "x-api-key: $CONTEXT_API_KEY" | jq -r .status curl -sS -X DELETE "$CONTEXT_BASE_URL/v1/files/$FILE_ID" -H "x-api-key: $CONTEXT_API_KEY"

What happened in this quickstart

  • The session is a task in the Context app. Its transcript is there, under the title you gave it.
  • The upload went to your Drive in the app. The session reads it from its inputs/ folder.
  • The stream was live from the moment it opened and carried nothing from before. List events returns the whole history.
  • Archiving ended the session and kept its history readable. To remove a session for good, use Delete a session with a key that has sessions:delete.

Next steps

  • Sessions explains what each session status means and how to rename and archive a session.
  • Stream events shows which frames the stream sends, how it ends and how to reconnect without losing events.
  • The tool approvals playbook shows how to approve or deny a tool call when a session goes idle with requires_action.