Skip to main content

Overview

Historia Diaria uses OpenRouter API to generate daily stories with AI. This guide walks you through setting up your API key and configuring the model.
OpenRouter provides access to multiple AI models with a single API key. You only need one account to use any available model.

Step 1: Create OpenRouter Account

1

Visit OpenRouter

Go to openrouter.ai and create a free account
2

Add Credits

Navigate to your account settings and add credits to your account. Most models cost only a few cents per story generation.
3

Generate API Key

In your OpenRouter dashboard:
  • Click on “API Keys”
  • Click “Create New Key”
  • Give it a descriptive name (e.g., “Historia Diaria”)
  • Copy the generated key immediately (you won’t see it again)

Step 2: Configure GitHub Secrets

Your API key must be stored as a GitHub secret to keep it secure.
1

Navigate to Repository Settings

Go to your GitHub repository → SettingsSecrets and variablesActions
2

Create New Secret

  • Click New repository secret
  • Name: OPENROUTER_API_KEY
  • Value: Paste your OpenRouter API key
  • Click Add secret
Never commit your API key directly in the code. Always use GitHub Secrets. The workflow accesses it securely through environment variables.

Step 3: How the API Key is Used

The workflow passes the secret to your Python script as an environment variable:
.github/workflows/actualizar.yml
- name: 4. Ejecutar tu script
  env:
    OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }}
  run: python generar_historia.py
Your script retrieves it safely:
generar_historia.py
import os

# Retrieve API key from environment variable
api_key = os.environ.get("OPENROUTER_API_KEY")

Model Selection

Historia Diaria uses the OpenRouter API to generate stories. You can choose from various models based on cost and quality.

Current Model

The default configuration uses:
generar_historia.py
response = requests.post(
    url="https://openrouter.ai/api/v1/chat/completions",
    headers={
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    },
    data=json.dumps({
        "model": "stepfun/step-3.5-flash:free",
        "messages": [{"role": "user", "content": prompt}]
    })
)

Available Models

Free Models

  • stepfun/step-3.5-flash:free (Current)
  • google/gemini-flash-1.5 (Free tier)
  • Limited rate limits

Premium Models

  • openai/gpt-4-turbo
  • anthropic/claude-3-opus
  • google/gemini-pro-1.5
  • Better quality, higher cost

Changing the Model

To use a different model, edit line 23 in generar_historia.py:
generar_historia.py
# Change this line:
"model": "stepfun/step-3.5-flash:free",

# To your preferred model:
"model": "openai/gpt-4-turbo",
Check OpenRouter’s model list for current pricing and availability.

Cost Considerations

Free Tier

The default stepfun/step-3.5-flash:free model is completely free but has:
  • Rate limits
  • Lower priority during peak times
  • May have quality variations
Typical costs for a 2-3 paragraph story generation:
  • google/gemini-flash-1.5: ~$0.002 per story
  • openai/gpt-3.5-turbo: ~$0.005 per story
  • Ideal for daily automation
  • openai/gpt-4-turbo: ~$0.08 per story
  • anthropic/claude-3-opus: ~$0.15 per story
  • Best quality for special occasions

Monthly Cost Estimation

For daily story generation (30 stories/month):
ModelCost per StoryMonthly Cost
Free models$0.00$0.00
Gemini Flash$0.002$0.06
GPT-3.5 Turbo$0.005$0.15
GPT-4 Turbo$0.08$2.40
Claude Opus$0.15$4.50

Error Handling

The script includes built-in error handling if the API fails:
generar_historia.py
try:
    response = requests.post(
        url="https://openrouter.ai/api/v1/chat/completions",
        headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
        data=json.dumps({"model": "stepfun/step-3.5-flash:free", "messages": [{"role": "user", "content": prompt}]})
    )
    respuesta_ia = response.json()['choices'][0]['message']['content']
except Exception as e:
    respuesta_ia = "<TITULO>Error</TITULO><HISTORIA>Fallo al conectar con la API.</HISTORIA><IMAGEN>error</IMAGEN>"
If the API fails, an error page is generated instead of stopping the workflow.

Next Steps

Customize Prompts

Learn how to customize the AI prompt and story format

GitHub Actions Setup

Configure the automation workflow

Build docs developers (and LLMs) love