Skip to main content
GET
/
specialities
/
:id
Get Specialty by ID
curl --request GET \
  --url https://api.example.com/specialities/:id
{
  "speciality": {
    "id": 123,
    "name": "<string>",
    "state": "<string>",
    "createdAt": "<string>",
    "updatedAt": "<string>"
  }
}

Overview

Retrieves a single medical specialty by its unique identifier.

Authentication

This endpoint requires authentication via JWT token. Include the token in the Authorization header.
Required: JWT token (any authenticated user) Middleware chain:
  • validatorJWT - Validates JWT token (backend/middlewares/validatorJWT.ts)
  • collectionErrors - Handles validation errors (backend/middlewares/collectionErrors.ts)
Authorization: No admin privileges required - available to all authenticated users.

Endpoint

GET /specialities/:id
Implementation: backend/controllers/speciality.ts:72

Path Parameters

id
number
required
The unique identifier of the specialty to retrieve.Validation: Must be a valid numeric ID that exists in the database.Examples:
  • 1 - Retrieves specialty with ID 1
  • 25 - Retrieves specialty with ID 25

Request Examples

curl -X GET "http://localhost:3000/specialities/1" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

Response

speciality
object
required
The specialty object.

Response Examples

{
  "speciality": {
    "id": 1,
    "name": "Cardiología",
    "state": "Activa",
    "createdAt": "2024-01-15T10:30:00.000Z",
    "updatedAt": "2024-01-15T10:30:00.000Z"
  }
}

Status Codes

Status CodeDescription
200Success - Returns the specialty object
401Unauthorized - Invalid or missing JWT token
404Not Found - Specialty with the given ID does not exist
500Server Error - Database or internal error

Implementation Details

Database Query

The endpoint uses Sequelize’s findByPk (find by primary key) method:
const { id } = req.params;

const speciality = await Speciality.findByPk(id)

if (!speciality) {
  res.status(404).json({
    msg: "No hay especialidad cargada en el sistema"
  })
  return
}

res.status(200).json({
  speciality
})
Source: backend/controllers/speciality.ts:76-91

Model Definition

The Speciality model is defined with the following schema:
export const Speciality = sequelize.define('Especialidades', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true
  },
  state: {
    type: DataTypes.ENUM(...Object.values(STATES_SPECIALITIES)),
    allowNull: false,
    defaultValue: STATES_SPECIALITIES.active
  }
})
Source: backend/models/speciality.ts:16-32

Common Use Cases

Fetch specialty details for display

const response = await fetch(`/specialities/${id}`, {
  headers: { Authorization: `Bearer ${token}` }
});

if (response.ok) {
  const { speciality } = await response.json();
  console.log(`Specialty: ${speciality.name} (${speciality.state})`);
} else {
  console.error('Specialty not found');
}

Pre-populate edit form

// Fetch specialty data before showing edit form
const response = await fetch(`/specialities/${id}`, {
  headers: { Authorization: `Bearer ${token}` }
});

const { speciality } = await response.json();

// Use data to populate form fields
setFormData({
  name: speciality.name,
  state: speciality.state
});

Verify specialty exists

try {
  const response = await fetch(`/specialities/${id}`, {
    headers: { Authorization: `Bearer ${token}` }
  });
  
  if (!response.ok) {
    throw new Error('Specialty not found');
  }
  
  const { speciality } = await response.json();
  return speciality;
} catch (error) {
  // Handle specialty not found
  console.error(error);
  return null;
}

Notes

This endpoint returns the specialty regardless of its state (active or inactive). If you need to filter by state, use the Get All Specialties endpoint with the state query parameter.
  • The endpoint does not validate the ID format in the route handler
  • Invalid IDs (non-numeric or non-existent) will return 404
  • Sequelize automatically handles type conversion for the numeric ID
  • The specialty object includes timestamps (createdAt and updatedAt) automatically added by Sequelize

Build docs developers (and LLMs) love