/

Project Token

Project Token

Your project token is the authentication credential that connects your Python application to your ByteHide Logs project.

Getting Your Token

  1. Log in to the ByteHide Dashboard
  2. Navigate to your Logs project
  3. Go to SettingsProject Token
  4. Copy your token (starts with bh_ prefix)

Setting the Project Token

Direct Configuration

Set the token directly in your code:

Python
from bytehide_logs import Log

Log.set_project_token("bh_your_project_token_here")

Note: This approach should only be used in development. For production, use environment variables.

Environment Variables

ByteHide Logs checks two environment variables in order:

  1. PROJECT_TOKEN - Custom environment variable
  2. BYTEHIDE_PROJECT_TOKEN - Standard ByteHide variable
Bash
# Using PROJECT_TOKEN
export PROJECT_TOKEN="bh_your_token"

# Or using BYTEHIDE_PROJECT_TOKEN
export BYTEHIDE_PROJECT_TOKEN="bh_your_token"

# Run your application
python app.py

Your application code:

Python
from bytehide_logs import Log, LogSettings

# Token will be automatically loaded from environment
Log.initialize(LogSettings(console_enabled=True))

In Python Code

Read the token from environment if set:

Python
import os
from bytehide_logs import Log, LogSettings

token = os.getenv("PROJECT_TOKEN") or os.getenv("BYTEHIDE_PROJECT_TOKEN")
if not token:
    raise ValueError("Project token not found in environment variables")

Log.set_project_token(token)
Log.initialize(LogSettings(console_enabled=True))

Security Best Practices

Never Hardcode Tokens

Avoid committing tokens to version control:

Python
# Bad - Never do this
Log.set_project_token("bh_abc123def456")

# Good - Use environment variables
Log.set_project_token(os.getenv("PROJECT_TOKEN"))

Use .env Files in Development

Create a .env file locally (never commit it):

Bash
# .env
PROJECT_TOKEN=bh_your_development_token

# .gitignore
.env

Load it in your development environment:

Bash
source .env
python app.py

Environment-Specific Tokens

Use different tokens for different environments:

Python
import os

def get_project_token():
    """Get appropriate token based on environment"""
    env = os.getenv("ENV", "development")
    
    if env == "production":
        return os.getenv("PRODUCTION_TOKEN")
    elif env == "staging":
        return os.getenv("STAGING_TOKEN")
    else:
        return os.getenv("DEVELOPMENT_TOKEN")

Log.set_project_token(get_project_token())

Rotate Tokens Regularly

Periodically rotate your project tokens for security:

  1. Generate a new token in the ByteHide Dashboard
  2. Update all applications with the new token
  3. Verify the new token is working
  4. Revoke the old token

Docker Configuration

Pass the token as an environment variable to your container:

DOCKERFILE
FROM python:3.9
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Run with token:

Bash
docker run -e PROJECT_TOKEN="bh_your_token" myapp

Or in docker-compose.yml:

YAML
version: '3'
services:
  app:
    build: .
    environment:
      - PROJECT_TOKEN=${PROJECT_TOKEN}

Run with:

Bash
PROJECT_TOKEN="bh_your_token" docker-compose up

Kubernetes Configuration

Store the token in a Kubernetes Secret:

Bash
kubectl create secret generic bytehide-secret --from-literal=project-token=bh_your_token

Reference in your deployment:

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  template:
    spec:
      containers:
      - name: app
        image: myapp:latest
        env:
        - name: PROJECT_TOKEN
          valueFrom:
            secretKeyRef:
              name: bytehide-secret
              key: project-token

Token Validation

Verify your token is correctly set and working:

Python
from bytehide_logs import Log, LogSettings

try:
    Log.set_project_token("bh_your_token")
    settings = LogSettings(console_enabled=True)
    Log.initialize(settings)
    Log.info("Token validation successful")
except Exception as e:
    print(f"Token validation failed: {e}")

Troubleshooting

"Invalid project token"

  • Verify the token is copied correctly from the dashboard
  • Ensure the token format matches (should start with bh_)
  • Check that the project hasn't been deleted

Token not loading from environment

Python
import os
print(f"PROJECT_TOKEN: {os.getenv('PROJECT_TOKEN')}")
print(f"BYTEHIDE_PROJECT_TOKEN: {os.getenv('BYTEHIDE_PROJECT_TOKEN')}")

Multiple environments

Use configuration files or separate settings by environment:

Python
import os
from pathlib import Path

def load_config():
    env = os.getenv("ENV", "development")
    config_file = Path(f"config.{env}.py")
    
    if config_file.exists():
        exec(config_file.read_text())
        return PROJECT_TOKEN

Next Steps

Previous
Quick Start