# @dimension-studios/ttk-proxy

CLI tool for TikTok Proxy API - A simple command-line interface to interact with the TikTok proxy service.

## Installation

```bash
npm install -g @dimension-studios/ttk-proxy
```

## Configuration

The CLI uses `conf` to store your API credentials. You can configure it in two ways:

### 1. Using Environment Variables (Recommended)

Set the following environment variables:

```bash
export TTK_FETCH_API_URL="https://your-api-url.com"
export TTK_FETCH_API_KEY="your-api-key"
export TTK_FETCH_API_SECRET="your-api-secret"
```

Then run the config command to save them:

```bash
ttk-proxy config
```

### 2. Using the CLI

```bash
ttk-proxy config:set apiUrl "https://your-api-url.com"
ttk-proxy config:set apiKey "your-api-key"
ttk-proxy config:set apiSecret "your-api-secret"
```

### View Current Configuration

```bash
ttk-proxy config --show
```

### Clear Configuration

```bash
ttk-proxy config --clear
```

## Usage

### List TikTok Proxy Tokens

List all available TikTok proxy tokens:

```bash
ttk-proxy list
```

### Search shops (by name or slug)

Search shops your API token can access (requires `GET /ttk-proxy/shops/search` on the Node API):

```bash
ttk-proxy shops coffee
```

### Call the TikTok Proxy API

Make a call to the TikTok proxy API with a JSON body:

```bash
ttk-proxy call '{"shopId":"your-shop-id","url":"https://api.tiktok.com/endpoint","method":"GET"}'
```

Example with more complex body:

```bash
ttk-proxy call '{
  "shopId": "shop-123",
  "url": "https://api.tiktok.com/v1/shops/products",
  "method": "GET",
  "headers": {
    "Content-Type": "application/json"
  },
  "unofficialApi": {
    "addCookieToHeaders": true,
    "signUrl": true
  }
}'
```

#### Base64 Support

You can also pass the JSON body as a base64-encoded string. The CLI will automatically detect and decode it:

```bash
# First encode your JSON to base64
echo -n '{"shopId":"shop-123","url":"https://api.tiktok.com/endpoint","method":"GET"}' | base64

# Then pass it directly to the CLI
ttk-proxy call "eyJzaG9wSWQiOiJzaG9wLTEyMyIsInVybCI6Imh0dHBzOi8vYXBpLnRpa3Rvay5jb20vZW5kcG9pbnQiLCJtZXRob2QiOiJHRVQifQ=="
```

This is useful when copying request bodies from logs or other sources where the JSON is already base64-encoded.

## Output

The CLI provides beautiful, color-coded output using `@rharkor/logger`:

- ✅ Success messages in green
- ❌ Error messages in red
- ℹ️ Info messages in blue
- ⚠️ Warning messages in yellow

Example output:

```
🚀 Calling TikTok Proxy API...
✅ Status: 200 OK

📦 Response:
{
  "code": 0,
  "data": {
    "products": [...]
  },
  "message": "success"
}
```

## API Reference

### Commands

#### `ttk-proxy call <body>`

Call the TikTok proxy API with a JSON body.

- **Arguments:**
  - `body` (string): Stringified JSON body for the request (can be raw JSON or base64-encoded JSON)

#### `ttk-proxy list`

List all TikTok proxy tokens.

#### `ttk-proxy shops <query>`

Search shops authorized for your API token (`GET /ttk-proxy/shops/search` on your Node API). Use at least 2 characters in `query`.

#### `ttk-proxy get-cookies`

Fetch cookies JSON for the given shop ID (calls `POST /ttk-proxy/get-cookies`).

Manage configuration.

- **Options:**
  - `-s, --show`: Show current configuration
  - `-c, --clear`: Clear all configuration

#### `ttk-proxy config:set <key> <value>`

Set a configuration value.

- **Arguments:**
  - `key` (string): Configuration key (`apiUrl`, `apiKey`, or `apiSecret`)
  - `value` (string): Configuration value

## Development

### Setup

```bash
# Install dependencies
pnpm install

# Build the package
pnpm run build

# Run type checking
pnpm run type-check

# Development mode (watch)
pnpm run dev
```

### Testing Locally

```bash
# Link the package globally
npm link

# Now you can use ttk-proxy command
ttk-proxy --help
```

## License

ISC

## Author

Dimension Studios

## Support

For issues and questions, please open an issue on the GitHub repository.

