Skip to main content

Environment Variables

The LoL Tracker Backend uses environment variables to manage sensitive configuration data. All environment variables are stored in a .env file and loaded using the dotenv package.

Loading Environment Variables

At the top of index.js, environment variables are loaded:
index.js
require('dotenv').config();
This line must be called before accessing any process.env variables to ensure they’re loaded correctly.

Required Variables

RIOT_API_KEY
string
required
Your Riot Games Developer API key. This is used to authenticate all requests to the Riot Games API.How to set it:
.env
RIOT_API_KEY=RGAPI-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
How it’s used in the code:
index.js
const apiKey = process.env.RIOT_API_KEY;
The .env file is included in .gitignore to prevent accidental exposure of your API key. Never commit this file to version control.

Server Configuration

The server configuration is defined in index.js and controls how the Express application runs.

Port Configuration

The server listens on port 3000 by default:
index.js
const PORT = 3000;

app.listen(PORT, () => {
    console.log(`Servidor corriendo en http://localhost:${PORT}`);
});
To run the server on a different port, modify the PORT constant:
index.js
const PORT = 8080; // Change to your desired port
Or make it configurable via environment variables:
index.js
const PORT = process.env.PORT || 3000;
Then add to .env:
.env
PORT=8080
RIOT_API_KEY=your_key_here

CORS Configuration

The server uses the cors middleware to handle Cross-Origin Resource Sharing:
index.js
const cors = require('cors');
app.use(cors());
Current behavior: Allows requests from any origin (suitable for development).
Default configuration allows all origins:
index.js
app.use(cors());
This is perfect for local development when your frontend and backend are on different ports.

JSON Body Parser

The server is configured to parse JSON request bodies:
index.js
app.use(express.json());
This middleware enables the server to automatically parse incoming JSON payloads in request bodies, making the data available in req.body.

API Request Configuration

The backend uses axios for making HTTP requests to the Riot Games API.

Axios Configuration

Every request to the Riot API includes authentication headers:
index.js
const apiKey = process.env.RIOT_API_KEY;

const usuario = await axios.get(
    `https://americas.api.riotgames.com/riot/account/v1/accounts/by-riot-id/${nombre}/${tag}`,
    { headers: { 'X-Riot-Token': apiKey } }
);
Key configuration elements:
  • Base URL: Different Riot API endpoints use different regional URLs (americas, la2, etc.)
  • Headers: All requests include X-Riot-Token header with the API key
  • Authentication: API key is loaded from process.env.RIOT_API_KEY

Regional Endpoints Used

The application connects to multiple Riot API regions:
https://americas.api.riotgames.com/riot/account/v1/accounts/by-riot-id/${nombre}/${tag}

Configuration Best Practices

Security

  • Never hardcode API keys in source code
  • Always use .env for sensitive data
  • Keep .env in .gitignore
  • Use different API keys for dev/prod

Performance

  • Implement request caching for frequently accessed data
  • Use connection pooling for database connections
  • Enable compression middleware for responses
  • Monitor API rate limits

Error Handling

  • Implement global error handler middleware
  • Log errors to monitoring service
  • Return user-friendly error messages
  • Never expose internal errors to clients

Environment Management

  • Create separate .env.development and .env.production
  • Document all required environment variables
  • Validate env vars on server startup
  • Use defaults for non-sensitive config

Example .env File

Create a .env file in your project root with the following structure:
.env
# Riot Games API Configuration
RIOT_API_KEY=RGAPI-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

# Server Configuration (optional)
PORT=3000
NODE_ENV=development

# CORS Configuration (optional)
ALLOWED_ORIGINS=http://localhost:5173,http://localhost:3001
Create a .env.example file (without real values) to commit to your repository as a template:
.env.example
RIOT_API_KEY=your_riot_api_key_here
PORT=3000

Verifying Configuration

To verify your configuration is working correctly:
1

Check environment variables are loaded

Add a console.log to verify (remove after testing):
console.log('API Key loaded:', process.env.RIOT_API_KEY ? 'Yes' : 'No');
2

Test the root endpoint

curl http://localhost:3000
Should return:
¡El servidor del LoL Tracker está vivo, Guillote!
3

Test API key authentication

Try a real API request:
curl http://localhost:3000/api/jugador/Faker/KR1
If configured correctly, you’ll get player data. If not, you’ll see an error.

Next Steps

Authentication Guide

Learn how to obtain and use Riot API keys

API Overview

Explore all available endpoints and their usage

Build docs developers (and LLMs) love