Autenticaci贸n

La API de SalonBookIt utiliza dos m茅todos de autenticaci贸n: API Keys para identificar tu negocio y JWT para autenticar clientes.

Resumen

P煤blico

API Key

Identifica tu negocio en cada petici贸n. Requerida para todos los endpoints.

  • Se obtiene desde el Dashboard
  • Se env铆a en el header X-API-Key
  • Prefijo hh_pub_ (p煤blica) o hh_sec_ (privada)
Autenticado

JWT Token

Autentica a un cliente espec铆fico. Requerido para endpoints privados.

  • Se obtiene v铆a /api/v1/auth/login/
  • Se env铆a en el header Authorization
  • Expira en 24 horas

API Keys

Tu API Key identifica tu negocio y debe incluirse en todas las peticiones a la API.

Obtener tu API Key

  1. Inicia sesi贸n en tu Dashboard
  2. Ve a Configuraci贸n → Integraciones
  3. Haz clic en Generar Nueva API Key
  4. Asigna un nombre descriptivo (ej: "Widget Web", "App M贸vil")
  5. Copia y guarda la API Key de forma segura

Usar la API Key

Incluye tu API Key en el header X-API-Key de cada petici贸n:

cURL
curl -X GET "https://app.salonbookit.com/api/v1/negocio/" \
  -H "X-API-Key: hh_pub_live_abc123def456..."
JavaScript
const response = await fetch('https://app.salonbookit.com/api/v1/servicios/', {
    headers: {
        'X-API-Key': 'hh_pub_live_abc123def456...'
    }
});
const data = await response.json();
Python
import requests

response = requests.get(
    'https://app.salonbookit.com/api/v1/servicios/',
    headers={'X-API-Key': 'hh_pub_live_abc123def456...'}
)
data = response.json()
PHP
$ch = curl_init('https://app.salonbookit.com/api/v1/servicios/');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'X-API-Key: hh_pub_live_abc123def456...'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$data = json_decode($response, true);

Tipos de API Key

Las API Keys tienen dos dimensiones: tipo (p煤blica/privada) y entorno (producci贸n/pruebas).

Por tipo

Prefijo Tipo Uso
hh_pub_ P煤blica Para widgets y frontend. Permisos limitados a lectura y crear reservas.
hh_sec_ Privada Para backend. No exponer en c贸digo cliente.

Por entorno

Sufijo Entorno Uso
_live_ Producci贸n Datos reales, transacciones reales
_test_ Pruebas Datos de prueba, sin cargos reales

Ejemplos de formatos completos:

  • hh_pub_live_... - P煤blica de producci贸n (para widgets en producci贸n)
  • hh_pub_test_... - P煤blica de pruebas (para desarrollo de widgets)
  • hh_sec_live_... - Privada de producci贸n (para tu backend)
  • hh_sec_test_... - Privada de pruebas (para desarrollo backend)
Seguridad de API Keys

Nunca incluyas tu API Key en c贸digo del lado del cliente que sea visible p煤blicamente. Para el widget, usamos una API Key con permisos limitados que solo permite operaciones de lectura y creaci贸n de reservas.

Autenticaci贸n JWT

Los endpoints que requieren autenticaci贸n de cliente utilizan tokens JWT. Estos endpoints incluyen:

  • Ver reservas del cliente
  • Ver/modificar perfil del cliente
  • Consultar puntos de fidelidad
  • Ver historial de pedidos

Obtener un token JWT

POST /api/v1/auth/login/

Request

cURL
curl -X POST "https://app.salonbookit.com/api/v1/auth/login/" \
  -H "X-API-Key: hh_pub_live_abc123..." \
  -H "Content-Type: application/json" \
  -d '{
    "email": "cliente@ejemplo.com",
    "password": "contrase帽a123"
  }'

Response (200 OK) - Cliente

JSON
{
  "success": true,
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
    "tipo": "cliente",
    "cliente": {
      "id": 123,
      "nombre": "Juan",
      "apellido": "Garc铆a",
      "email": "cliente@ejemplo.com",
      "telefono": "+34612345678",
      "puntos": 150,
      "nivel": "gold"
    }
  }
}

Response (200 OK) - Staff

Si las credenciales corresponden a un miembro del staff:

JSON
{
  "success": true,
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
    "tipo": "staff",
    "staff": {
      "id": 1,
      "nombre": "Mar铆a",
      "apellido": "L贸pez",
      "email": "maria@salon.com",
      "telefono": "+34612345678",
      "rol": "admin",
      "permisos": {
        "puede_ver_reservas": true,
        "puede_crear_reservas": true,
        "puede_ver_clientes": true
      }
    }
  }
}

Usar el token JWT

Incluye el token en el header Authorization con el prefijo Bearer:

cURL
curl -X GET "https://app.salonbookit.com/api/v1/cliente/reservas/" \
  -H "X-API-Key: hh_pub_live_abc123..." \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."

Renovar el token

Los tokens expiran en 24 horas. Usa el endpoint de refresh para obtener uno nuevo:

POST /api/v1/auth/refresh/
cURL
curl -X POST "https://app.salonbookit.com/api/v1/auth/refresh/" \
  -H "X-API-Key: hh_pub_live_abc123..." \
  -H "Authorization: Bearer TOKEN_ACTUAL"

Rate Limiting

La API tiene l铆mites de peticiones para garantizar un servicio estable:

L铆mite Ventana Descripci贸n
120 requests 1 minuto L铆mite general por API Key
20 intentos 15 minutos Login (por IP)
10 intentos 1 hora Registro (por IP)

Headers de Rate Limit

Cada respuesta incluye headers informativos:

Headers
X-RateLimit-Limit: 120
X-RateLimit-Remaining: 115
X-RateLimit-Reset: 1704067200
Header Descripci贸n
X-RateLimit-Limit N煤mero m谩ximo de requests permitidos
X-RateLimit-Remaining Requests restantes en la ventana actual
X-RateLimit-Reset Timestamp Unix cuando se reinicia el contador

Error 429: Too Many Requests

Si excedes el l铆mite, recibir谩s un error 429:

JSON
{
  "success": false,
  "error": "Has excedido el l铆mite de requests. Intenta de nuevo en 45 segundos.",
  "code": "RATE_LIMIT_EXCEEDED"
}

Errores de autenticaci贸n

C贸digo HTTP Status Descripci贸n
MISSING_API_KEY 401 No se incluy贸 el header X-API-Key
INVALID_API_KEY 401 API Key no v谩lida o no encontrada
API_KEY_DISABLED 403 La API Key ha sido desactivada
API_KEY_EXPIRED 403 La API Key ha expirado
INVALID_TOKEN 401 JWT Token inv谩lido o malformado
TOKEN_EXPIRED 401 JWT Token expirado
INVALID_CREDENTIALS 401 Email o contrase帽a incorrectos