Mejores Prácticas de API
Guía para usar la API de TeamDay de forma eficiente y segura.
Seguridad
1. Protege Tus Tokens
# ❌ Nunca hagas esto
api_token = "td_abc123def456" # En código
curl -H "Authorization: Bearer td_abc123" https://...
# ✅ Haz esto
export TEAMDAY_API_TOKEN="td_abc123def456"
curl -H "Authorization: Bearer $TEAMDAY_API_TOKEN" https://...
2. Rota Tokens Regularmente
- Crea nuevo token cada 90 días
- Revoca los antiguos
- Usa diferentes tokens para diferentes aplicaciones
3. Usa HTTPS Siempre
- Todos los endpoints requieren HTTPS
- Nunca uses HTTP
Rendimiento
1. Implementa Caché
const cache = new Map();
function getAgentCached(agentId, maxAge = 5 * 60 * 1000) {
const cached = cache.get(agentId);
if (cached && Date.now() - cached.time < maxAge) {
return cached.data;
}
const data = fetchAgent(agentId);
cache.set(agentId, { data, time: Date.now() });
return data;
}
2. Usa Paginación
# Obtén 100 resultados a la vez
curl "https://us.teamday.ai/api/v1/agents?limit=100"
3. Minimiza Solicitudes
- Obtén datos en una sola solicitud
- Evita loops de solicitudes
Manejo de Errores
1. Reintenta con Backoff Exponencial
import time
def retry_request(url, token, max_attempts=3):
for attempt in range(max_attempts):
try:
return requests.get(url, headers={'Authorization': f'Bearer {token}'})
except RequestException as e:
wait_time = 2 ** attempt
print(f"Attempt {attempt + 1} failed. Retrying in {wait_time}s...")
time.sleep(wait_time)
2. Maneja Rate Limits
async function withRateLimit(fn) {
try {
return await fn();
} catch (error) {
if (error.status === 429) {
await sleep(60000); // Espera 1 minuto
return fn();
}
throw error;
}
}
3. Registra Errores
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
except Exception as e:
logger.error(f"API request failed: {e}")
raise
Monitoreo
1. Registra Uso de Tokens
curl -X GET "https://us.teamday.ai/api/v1/executions?limit=100" \
-H "Authorization: Bearer $TEAMDAY_API_TOKEN" \
| jq '.[] | {id, agentId, usage}'
2. Monitorea Costos
- Rastrea tokens usados por agente
- Optimiza prompts para reducir uso
Próximos Pasos
- Lee Ejemplos
- Explora Referencia de Endpoints