> ## Documentation Index
> Fetch the complete documentation index at: https://docs.strike.markets/llms.txt
> Use this file to discover all available pages before exploring further.

# Generate API Key

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST "https://api.strike.markets/api-key/generate" \
    -H "X-API-Key: stk_service_key_or_existing_key" \
    -H "X-Wallet-Address: 0x742d35cc6c6c7532b1140da4c8a2f6c8ecfc9b46" \
    -H "Content-Type: application/json"
  ```

  ```python Python theme={null}
  import requests
  import os

  headers = {
      'X-API-Key': os.environ['STRIKE_SERVICE_KEY'],
      'X-Wallet-Address': '0x742d35cc6c6c7532b1140da4c8a2f6c8ecfc9b46',
      'Content-Type': 'application/json'
  }

  response = requests.post(
      'https://api.strike.markets/api-key/generate',
      headers=headers
  )

  if response.status_code == 200:
      data = response.json()
      print(f"New API Key: {data['api_key']}")
      print("IMPORTANT: Save this key - it won't be shown again!")
  ```

  ```typescript TypeScript theme={null}
  const response = await fetch('https://api.strike.markets/api-key/generate', {
    method: 'POST',
    headers: {
      'X-API-Key': process.env.STRIKE_SERVICE_KEY,
      'X-Wallet-Address': '0x742d35cc6c6c7532b1140da4c8a2f6c8ecfc9b46',
      'Content-Type': 'application/json'
    }
  });

  if (response.ok) {
    const data = await response.json();
    console.log(`New API Key: ${data.api_key}`);
    console.log("IMPORTANT: Save this key - it won't be shown again!");
  }
  ```
</RequestExample>

<ResponseExample>
  ```json Success Response theme={null}
  {
    "success": true,
    "api_key": "stk_abc123def456ghi789jkl012mno345pqr678stu901vwx234yz",
    "wallet_address": "0x742d35cc6c6c7532b1140da4c8a2f6c8ecfc9b46",
    "created_at": "2024-01-15T14:30:00Z",
    "message": "API key generated successfully. Save this key - it won't be shown again!"
  }
  ```

  ```json Error - Key Already Exists theme={null}
  {
    "error": "API_KEY_EXISTS",
    "message": "An API key already exists for this wallet. Revoke it first to generate a new one."
  }
  ```

  ```json Error - Invalid Wallet theme={null}
  {
    "error": "INVALID_WALLET",
    "message": "Invalid wallet address format"
  }
  ```
</ResponseExample>

## Description

Generate a new API key for a wallet address. Each wallet can only have one active API key at a time. If an API key already exists for the wallet, it must be revoked first before generating a new one.

## Important Notes

<Warning>
  **Save your API key immediately!** The API key is only shown once during generation. It cannot be retrieved later. If you lose your key, you'll need to revoke it and generate a new one.
</Warning>

## Authentication

This endpoint requires either:

* A service key (used by the frontend) with the wallet address in the `X-Wallet-Address` header
* An existing valid API key for the same wallet (for regeneration after revocation)

## Request Headers

| Header           | Type   | Required    | Description                     |
| ---------------- | ------ | ----------- | ------------------------------- |
| X-API-Key        | string | Yes         | Service key or existing API key |
| X-Wallet-Address | string | Conditional | Required when using service key |
| Content-Type     | string | Yes         | Must be `application/json`      |

## Response Fields

| Field           | Type    | Description                                 |
| --------------- | ------- | ------------------------------------------- |
| success         | boolean | Whether the operation was successful        |
| api\_key        | string  | The newly generated API key (save this!)    |
| wallet\_address | string  | The wallet address associated with this key |
| created\_at     | string  | ISO 8601 timestamp of key creation          |
| message         | string  | Informational message about the operation   |

## Error Codes

| Code             | Description                               |
| ---------------- | ----------------------------------------- |
| API\_KEY\_EXISTS | An API key already exists for this wallet |
| INVALID\_WALLET  | The provided wallet address is invalid    |
| UNAUTHORIZED     | Missing or invalid authentication         |
| INTERNAL\_ERROR  | Server error during key generation        |

## Rate Limiting

This endpoint is rate-limited to prevent abuse:

* 5 requests per minute per wallet address
* 20 requests per hour per wallet address

## Example Integration

```python theme={null}
class StrikeAPIClient:
    def __init__(self, service_key: str):
        self.service_key = service_key
        self.base_url = "https://api.strike.markets"
    
    def generate_api_key(self, wallet_address: str) -> str:
        """Generate a new API key for the given wallet"""
        
        headers = {
            'X-API-Key': self.service_key,
            'X-Wallet-Address': wallet_address,
            'Content-Type': 'application/json'
        }
        
        response = requests.post(
            f"{self.base_url}/api-key/generate",
            headers=headers
        )
        
        if response.status_code == 200:
            data = response.json()
            # In production, save this key securely
            return data['api_key']
        elif response.status_code == 409:
            raise Exception("API key already exists. Revoke it first.")
        else:
            raise Exception(f"Failed to generate key: {response.json()}")

# Usage
client = StrikeAPIClient(os.environ['STRIKE_SERVICE_KEY'])
api_key = client.generate_api_key("0x742d35cc6c6c7532b1140da4c8a2f6c8ecfc9b46")
print(f"Generated API Key: {api_key}")
# Save this key securely!
```
