API Examples

Practical examples of common API operations with the Mahalo Health Platform API.

Authentication

// Authenticate and get an access token
const getAccessToken = async () => {
  const response = await fetch('https://api.mahalo.health/v1/auth/token', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      client_id: 'YOUR_CLIENT_ID',
      client_secret: 'YOUR_CLIENT_SECRET',
      grant_type: 'client_credentials'
    })
  });
  
  const data = await response.json();
  return data.access_token;
};

// Use the token for API requests
const fetchPatientData = async (patientId) => {
  const token = await getAccessToken();
  
  const response = await fetch(`https://api.mahalo.health/v1/patients/${patientId}`, {
    headers: {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
    }
  });
  
  return await response.json();
};

Fetching Data

// Get a list of patients
const getPatients = async (limit = 10, offset = 0) => {
  const token = await getAccessToken();
  
  const response = await fetch(
    `https://api.mahalo.health/v1/patients?limit=${limit}&offset=${offset}`, 
    {
      headers: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
      }
    }
  );
  
  return await response.json();
};

// Get a specific patient's health metrics
const getPatientMetrics = async (patientId, startDate, endDate) => {
  const token = await getAccessToken();
  
  const response = await fetch(
    `https://api.mahalo.health/v1/patients/${patientId}/metrics?` + 
    `start_date=${startDate}&end_date=${endDate}`, 
    {
      headers: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
      }
    }
  );
  
  return await response.json();
};

Creating Resources

// Create a new appointment
const createAppointment = async (patientId, appointmentData) => {
  const token = await getAccessToken();
  
  const response = await fetch('https://api.mahalo.health/v1/appointments', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      patient_id: patientId,
      ...appointmentData
    })
  });
  
  return await response.json();
};

// Example usage
createAppointment('patient_123', {
  provider_id: 'provider_456',
  start_time: '2023-06-15T10:00:00Z',
  end_time: '2023-06-15T10:30:00Z',
  appointment_type: 'follow_up',
  notes: 'Follow-up appointment for medication review'
});

Updating Resources

// Update patient information
const updatePatient = async (patientId, patientData) => {
  const token = await getAccessToken();
  
  const response = await fetch(`https://api.mahalo.health/v1/patients/${patientId}`, {
    method: 'PATCH',
    headers: {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(patientData)
  });
  
  return await response.json();
};

// Example usage
updatePatient('patient_123', {
  email: 'newemail@example.com',
  phone_number: '+1-555-123-4567',
  address: {
    street: '123 Main St',
    city: 'San Francisco',
    state: 'CA',
    zip: '94105'
  }
});

Deleting Resources

// Cancel an appointment
const cancelAppointment = async (appointmentId, reason) => {
  const token = await getAccessToken();
  
  const response = await fetch(`https://api.mahalo.health/v1/appointments/${appointmentId}/cancel`, {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      cancellation_reason: reason
    })
  });
  
  return await response.json();
};

// Example usage
cancelAppointment('appointment_789', 'Patient requested rescheduling');