Skip to main content

Treatments API

The Treatments API allows you to manage the catalog of dental services offered by the clinic. Services can include consultations, procedures, treatments, and any billable items.
Services are also referred to as “treatments” in the API. The endpoint is /api/treatments but manages the services catalog.

Endpoints

List All Services

Retrieve all services/treatments in the catalog.
GET /api/treatments
Authentication Required: Yes (any authenticated user) Response
services
array
List of all services in the catalog
Example Request
curl -X GET http://localhost:5173/api/treatments \
  -H "Cookie: session=your_session_cookie"
Example Response
{
  "services": [
    {
      "id": 1,
      "name": "Consulta general",
      "description": "Consulta odontológica general",
      "price": 500.00,
      "created_at": "2024-01-15T10:00:00Z"
    },
    {
      "id": 2,
      "name": "Limpieza dental",
      "description": "Profilaxis completa",
      "price": 800.00,
      "created_at": "2024-01-15T10:00:00Z"
    }
  ]
}

Create Service

Create a new service in the catalog.
POST /api/treatments
Authentication Required: Yes (admin only) Permission Required: MANAGE_TREATMENTS Request Body
name
string
required
Service name
description
string
Service description (optional)
price
number
required
Service price (decimal value)
Example Request
curl -X POST http://localhost:5173/api/treatments \
  -H "Content-Type: application/json" \
  -H "Cookie: session=admin_session_cookie" \
  -d '{
    "name": "Resina estética",
    "description": "Restauración con resina composite",
    "price": 1200.00
  }'
Example Response
{
  "success": true,
  "id": 15
}
Error Responses
  • 400 Bad Request - Missing required fields (name or price)
  • 401 Unauthorized - Not authenticated or not admin
  • 500 Internal Server Error - Database error

Update Service

Update an existing service.
PUT /api/treatments
Authentication Required: Yes (admin only) Permission Required: MANAGE_TREATMENTS Request Body
id
integer
required
Service ID to update
name
string
required
Updated service name
description
string
Updated description (optional)
price
number
required
Updated price
Example Request
curl -X PUT http://localhost:5173/api/treatments \
  -H "Content-Type: application/json" \
  -H "Cookie: session=admin_session_cookie" \
  -d '{
    "id": 15,
    "name": "Resina estética - Anterior",
    "description": "Restauración con resina composite en diente anterior",
    "price": 1500.00
  }'
Example Response
{
  "success": true,
  "message": "Servicio actualizado"
}

Delete Service

Delete a service from the catalog.
DELETE /api/treatments
Authentication Required: Yes (admin only) Permission Required: MANAGE_TREATMENTS Request Body
id
integer
required
Service ID to delete
Example Request
curl -X DELETE http://localhost:5173/api/treatments \
  -H "Content-Type: application/json" \
  -H "Cookie: session=admin_session_cookie" \
  -d '{"id": 15}'
Example Response
{
  "success": true,
  "message": "Servicio eliminado"
}
Deleting a service may affect existing patient budgets and financial records that reference this service. Consider deactivating instead of deleting if historical data integrity is important.

Database Schema

Services are stored in the services table:
ColumnTypeDescription
idINTPrimary key, auto-increment
nameVARCHAR(100)Service name
descriptionTEXTService description
priceDECIMAL(10,2)Service price
created_atTIMESTAMPCreation timestamp

Stored Procedures

The API uses these stored procedures:
  • sp_list_services() - Lists all services
  • sp_create_service(name, description, price) - Creates a new service
  • sp_update_service(id, name, description, price) - Updates a service
  • sp_delete_service(id) - Deletes a service

Use Cases

Patient Budget Creation

Services are used when creating budgets for patients:
// Fetch available services
const response = await fetch('/api/treatments');
const { services } = await response.json();

// Create budget with selected services
const budget = {
  patient_id: 123,
  items: [
    { service_id: services[0].id, quantity: 1, price: services[0].price },
    { service_id: services[2].id, quantity: 2, price: services[2].price }
  ]
};

Financial Reporting

Services appear in financial reports to show revenue by service type:
// Get service income report
const response = await fetch('/api/reports?type=services');
const report = await response.json();

Reports API

Service revenue and performance reports

Patients API

Patient budgets and treatment plans

Error Messages

Common error messages returned by this endpoint:
MessageCause
”No autorizado”User not authenticated or not admin
”Faltan datos”Missing required fields (name, price, or id)
“ID faltante”Missing service ID in DELETE request
”Error al cargar servicios”Database error on GET
”Error al crear servicio”Database error on POST
”Error al actualizar servicio”Database error on PUT
”Error al eliminar servicio”Database error on DELETE

Source Code Reference

Implementation: src/routes/api/treatments/+server.js

Build docs developers (and LLMs) love