# How the Agent-Reach `configure` Command Parses Different Twitter Cookie Formats

> Learn how the Agent-Reach configure command parses various Twitter cookie formats, accepting header strings or bearer tokens to extract auth_token and ct0 values.

- Repository: [Pnant/Agent-Reach](https://github.com/Panniantong/Agent-Reach)
- Tags: internals
- Published: 2026-07-14

---

**The `configure` command accepts Twitter authentication cookies as either a full HTTP cookie-header string or two separate bearer tokens, extracting the `auth_token` and `ct0` values via the `_parse_twitter_cookie_input()` helper in [`agent_reach/cli.py`](https://github.com/Panniantong/Agent-Reach/blob/main/agent_reach/cli.py).**

The open-source Agent-Reach framework (available at `Panniantong/Agent-Reach`) provides a CLI tool for managing social media channels, including Twitter authentication. When users run `agent-reach configure twitter-cookies <value>`, the system handles multiple Twitter cookie formats through a flexible parsing algorithm that accommodates both copy-paste workflows from browser dev tools and manual token entry.

## Supported Twitter Cookie Input Formats

The CLI recognizes two distinct input patterns for Twitter session data. This dual-format support allows users to paste raw cookie headers directly from browser developer tools or enter the two critical tokens individually.

### Full Cookie-Header Strings

The parser detects a complete cookie header by checking for the simultaneous presence of `auth_token=` and `ct0=` substrings. When found, the input is split on semicolons or whitespace, and each segment is scanned for these specific keys. The values following the first `=` delimiter are extracted and stored. This format accepts strings like `auth_token=abc123; ct0=def456; other=ignore`, ignoring extraneous cookie attributes.

### Separate Token Arguments

If the input contains no `=` characters and consists of exactly two whitespace-separated tokens, the parser interprets the first value as `auth_token` and the second as `ct0`. This format requires values like `A1B2C3D4E5F6 G7H8I9J0K1L2` without any key prefixes or delimiters.

## Parsing Implementation in [`agent_reach/cli.py`](https://github.com/Panniantong/Agent-Reach/blob/main/agent_reach/cli.py)

The parsing logic resides in the `_parse_twitter_cookie_input()` function, defined around line 54 of [`agent_reach/cli.py`](https://github.com/Panniantong/Agent-Reach/blob/main/agent_reach/cli.py). The `configure` sub-command dispatches to this helper at lines 72-77 when `args.key == "twitter-cookies"`.

### Header Detection Logic (Lines 59-66)

The function first checks if the input string contains both `auth_token=` and `ct0=`. If this condition evaluates to true, the algorithm treats the input as a compound cookie header. It iterates through segments split by semicolons or whitespace, extracts the substring following the first `=` for each target key, and assigns these to the respective return variables.

### Dual Token Parsing (Lines 66-71)

When the header pattern is not detected, the function evaluates whether the input lacks `=` characters and splits into exactly two tokens. Upon validation, it maps the first token to `auth_token` and the second to `ct0`. This branch handles the space-separated token format without requiring additional parsing logic.

### Configuration Storage

Upon successful extraction, the CLI stores the parsed values as `twitter_auth_token` and `twitter_ct0` in the user's configuration file, typically located at `~/.agent-reach/config.yaml`. The system optionally validates these credentials against the `twitter-cli` integration to ensure active session validity before completing the configuration.

## Usage Examples

The following commands demonstrate both supported input methods:

```bash

# Format 1: Separate tokens (recommended for manual entry)

agent-reach configure twitter-cookies A1B2C3D4E5F6 G7H8I9J0K1L2

```

```bash

# Format 2: Full cookie header (copied from browser dev tools)

agent-reach configure twitter-cookies "auth_token=abc123; ct0=def456; other=ignore"

```

Both invocations invoke `_parse_twitter_cookie_input()`, which returns a tuple of `(auth_token, ct0)` for persistence.

## Error Handling and Validation

If the input matches neither the header pattern nor the dual-token pattern, the function returns `(None, None)`. The CLI detects these null values and prints an error message detailing the accepted formats, preventing malformed credentials from entering the configuration store. The test suite in [`tests/test_cli.py`](https://github.com/Panniantong/Agent-Reach/blob/main/tests/test_cli.py) verifies that both valid formats parse correctly and that invalid inputs trigger appropriate error messaging.

## Summary

- The `configure` command in Agent-Reach supports two Twitter cookie formats: full semicolon-separated headers and space-separated token pairs.
- Parsing logic in [`agent_reach/cli.py`](https://github.com/Panniantong/Agent-Reach/blob/main/agent_reach/cli.py) (lines 54-71) detects formats by checking for `auth_token=` and `ct0=` substrings or validating two argument tokens.
- Extracted values are stored as `twitter_auth_token` and `twitter_ct0` in the YAML configuration file.
- The implementation includes error handling that rejects malformed inputs with clear format requirements.

## Frequently Asked Questions

### What happens if I paste a cookie string that contains other attributes besides `auth_token` and `ct0`?

The parser only extracts the values for `auth_token` and `ct0`, ignoring all other cookie attributes in the string. As long as both required keys are present, extraneous data does not affect the extraction process.

### Can I use single quotes or other delimiters when entering separate tokens?

No, the two-token format requires exactly two whitespace-separated values without any `=` characters or quotes. The parser checks for the absence of `=` to distinguish this format from the full header format.

### Where does Agent-Reach store the parsed Twitter cookies?

After validation, the credentials are written to `~/.agent-reach/config.yaml` under the keys `twitter_auth_token` and `twitter_ct0`, as managed by [`agent_reach/config.py`](https://github.com/Panniantong/Agent-Reach/blob/main/agent_reach/config.py).

### How can I verify that my cookies were parsed correctly?

You can inspect the configuration file directly or check the CLI output for confirmation messages. The test file [`tests/test_twitter_channel.py`](https://github.com/Panniantong/Agent-Reach/blob/main/tests/test_twitter_channel.py) demonstrates how the stored values are retrieved and used for API authentication, providing a reference for expected storage behavior.