# Client Configuration

WebClient initialization and configuration options for authentication, networking, and behavior customization.

## Capabilities

### WebClient Constructor

Creates a new WebClient instance with optional authentication token and configuration options.

```typescript { .api }
/**
 * Creates a new WebClient instance
 * @param token - Slack bot or user token (starts with xoxb- or xoxp-)
 * @param options - Configuration options for the client
 */
class WebClient {
  constructor(token?: string, options?: WebClientOptions);
}
```

**Usage Examples:**

```typescript
import { WebClient, LogLevel } from "@slack/web-api";

// Basic initialization with token
const web = new WebClient(process.env.SLACK_BOT_TOKEN);

// With configuration options
const web = new WebClient(process.env.SLACK_BOT_TOKEN, {
  logLevel: LogLevel.DEBUG,
  timeout: 30000,
  maxRequestConcurrency: 10
});

// Without token (must pass token with each call)
const web = new WebClient();
const result = await web.apiCall('auth.test', { token: 'xoxb-...' });
```

### WebClientOptions Interface

Configuration options for customizing WebClient behavior.

```typescript { .api }
interface WebClientOptions {
  /** Base URL for API requests (default: https://slack.com/api/) */
  slackApiUrl?: string;
  /** Custom logger instance */
  logger?: Logger;
  /** Logging verbosity level */
  logLevel?: LogLevel;
  /** Maximum concurrent requests (default: 100) */
  maxRequestConcurrency?: number;
  /** Retry policy configuration */
  retryConfig?: RetryOptions;
  /** HTTP agent for requests */
  agent?: Agent;
  /** TLS/SSL configuration */
  tls?: TLSOptions;
  /** Request timeout in milliseconds */
  timeout?: number;
  /** Whether to reject rate-limited calls instead of retrying */
  rejectRateLimitedCalls?: boolean;
  /** Default HTTP headers to include in all requests */
  headers?: Record<string, string>;
  /** Slack team/workspace ID */
  teamId?: string;
  /** Allow absolute URLs in method names (default: true) */
  allowAbsoluteUrls?: boolean;
  /** Include original error in exceptions (default: true) */
  attachOriginalToWebAPIRequestError?: boolean;
  /** Custom request interceptor function */
  requestInterceptor?: RequestInterceptor;
  /** Custom HTTP adapter configuration */
  adapter?: AdapterConfig;
}
```

### Logging Configuration

Configure logging behavior for debugging and monitoring.

```typescript { .api }
interface Logger {
  debug(...msgs: any[]): void;
  info(...msgs: any[]): void;
  warn(...msgs: any[]): void;
  error(...msgs: any[]): void;
  setLevel(level: LogLevel): void;
  setName(name: string): void;
}

enum LogLevel {
  ERROR = 'error',
  WARN = 'warn', 
  INFO = 'info',
  DEBUG = 'debug'
}
```

**Usage Examples:**

```typescript
import { WebClient, LogLevel } from "@slack/web-api";

// Built-in logging levels
const web = new WebClient(token, {
  logLevel: LogLevel.DEBUG // Will log all requests and responses
});

// Custom logger
const customLogger = {
  debug: (msg) => console.log(`[DEBUG] ${msg}`),
  info: (msg) => console.log(`[INFO] ${msg}`),
  warn: (msg) => console.warn(`[WARN] ${msg}`),
  error: (msg) => console.error(`[ERROR] ${msg}`)
};

const web = new WebClient(token, { logger: customLogger });
```

### Retry Configuration

Configure automatic retry behavior for failed requests.

```typescript { .api }
interface RetryOptions {
  /** Number of retry attempts */
  retries?: number;
  /** Factor by which delay increases after each retry */
  factor?: number;
  /** Minimum delay between retries in milliseconds */
  minTimeout?: number;
  /** Maximum delay between retries in milliseconds */
  maxTimeout?: number;
  /** Whether to randomize retry delays */
  randomize?: boolean;
}
```

**Pre-defined Retry Policies:**

```typescript { .api }
/** Default policy: 10 retries over approximately 30 minutes */
const tenRetriesInAboutThirtyMinutes: RetryOptions;

/** Shorter policy: 5 retries over 5 minutes */
const fiveRetriesInFiveMinutes: RetryOptions;

/** Fast policy for testing scenarios */
const rapidRetryPolicy: RetryOptions;
```

**Usage Examples:**

```typescript
import { WebClient, retryPolicies } from "@slack/web-api";

// Use pre-defined policy
const web = new WebClient(token, {
  retryConfig: retryPolicies.fiveRetriesInFiveMinutes
});

// Custom retry configuration
const web = new WebClient(token, {
  retryConfig: {
    retries: 3,
    factor: 2,
    minTimeout: 1000,
    maxTimeout: 10000,
    randomize: true
  }
});
```

### Network Configuration

Configure HTTP behavior including timeouts, agents, and TLS settings.

```typescript { .api }
interface TLSOptions extends SecureContextOptions {
  /** Custom TLS/SSL configuration */
}

type RequestInterceptor = (config: RequestConfig) => RequestConfig;
type AdapterConfig = AxiosAdapter;
```

**Usage Examples:**

```typescript
import { WebClient } from "@slack/web-api";
import { Agent } from "node:http";

// Custom HTTP agent with connection pooling
const agent = new Agent({
  keepAlive: true,
  maxSockets: 25
});

const web = new WebClient(token, {
  agent,
  timeout: 30000, // 30 second timeout
  headers: {
    'User-Agent': 'MySlackApp/1.0.0'
  }
});

// Request interceptor for custom headers
const web = new WebClient(token, {
  requestInterceptor: (config) => {
    config.headers['X-Request-ID'] = generateRequestID();
    return config;
  }
});
```

### Rate Limiting Configuration

Configure how the client handles rate limiting from Slack's API.

```typescript { .api }
/** Whether to reject rate-limited calls instead of automatically retrying */
rejectRateLimitedCalls?: boolean;
```

**Usage Examples:**

```typescript
import { WebClient, WebClientEvent } from "@slack/web-api";

// Handle rate limiting manually
const web = new WebClient(token, {
  rejectRateLimitedCalls: true
});

// Listen for rate limit events
web.on(WebClientEvent.RATE_LIMITED, (retryAfter) => {
  console.log(`Rate limited. Retry after ${retryAfter} seconds`);
});

// Automatic retry (default behavior)
const web = new WebClient(token, {
  rejectRateLimitedCalls: false // Default
});
```

## Types

```typescript { .api }
enum WebClientEvent {
  RATE_LIMITED = 'rate_limited'
}

type RequestConfig = InternalAxiosRequestConfig;
```