Chiave API

Autenticazione

Tutte le richieste API richiedono autenticazione tramite chiave API. La tua chiave API deve essere inclusa negli header della richiesta.

Header chiave API

Includi la tua chiave API nell'header x-api-key:

Richiestabash
curl -X GET "https://www.strompreise-schweiz.ch/api/v1/tariffs" \
  -H "x-api-key: YOUR_API_KEY"

Esempio

JavaScriptjavascript
// JavaScript / Node.js
const response = await fetch('https://www.strompreise-schweiz.ch/api/v1/tariffs', {
  method: 'GET',
  headers: {
    'x-api-key': process.env.API_KEY,
    'Content-Type': 'application/json'
  }
});

const data = await response.json();
console.log(data);
Pythonpython
# Python
import requests

response = requests.get(
    'https://www.strompreise-schweiz.ch/api/v1/tariffs',
    headers={
        'x-api-key': 'YOUR_API_KEY',
        'Content-Type': 'application/json'
    }
)

data = response.json()
print(data)

Ottenere una chiave API

Per ottenere una chiave API, devi:

  1. Creare un account su Strompreise Schweiz
  2. Navigare alle impostazioni della tua organizzazione
  3. Andare alla sezione Chiavi API
  4. Generare una nuova chiave API

Best practice di sicurezza

  • Non esporre mai la tua chiave API nel codice lato client o repository pubblici
  • Ruotare periodicamente le tue chiavi API
  • Usare variabili d'ambiente per memorizzare le chiavi API
  • Ogni organizzazione dovrebbe usare chiavi API separate per diverse applicazioni

Risposta non autorizzata

Se la tua chiave API è invalida o mancante, riceverai una risposta 401:

Rispostajson
{
  "success": false,
  "error": "Unauthorized",
  "code": "UNAUTHORIZED"
}
Autenticazione | Strompreise Schweiz