{
  "info": {
    "_postman_id": "1413ff97-9b15-480b-a845-251e4870010b",
    "name": "Innowell FHIR API: Backend Services",
    "description": "Server-to-server (SMART Backend Services) access to the Innowell FHIR API.\n\n1. Import this collection and the matching environment, and select the environment.\n2. Fill in client_id, client_secret and tenant_id in the environment.\n3. Run any request. A token is fetched automatically when needed and reused until shortly before it expires (tokens last 300 seconds).\n\nRun 'Search patients' and 'Search practitioners' first: they fill in the sample ids and names the other requests use.\n\nGuide: https://developers.innowell.org/guides/smart-backend-services",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "auth": {
    "type": "bearer",
    "bearer": [
      {
        "key": "token",
        "value": "{{access_token}}",
        "type": "string"
      }
    ]
  },
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "exec": [
          "// Gets a token automatically before any request when there is none, it is about to",
          "// expire, or the settings changed, so you only need to fill in the environment.",
          "const cryptoJs = require('crypto-js');",
          "const SAMPLE_VARS = ['patient_id', 'innowell_patient_id', 'patient_name', 'practitioner_id', 'practitioner_name'];",
          "const settingsHash = cryptoJs.SHA256(JSON.stringify(",
          "  ['base_url', 'fhir_base', 'client_id', 'client_secret', 'tenant_id', 'scope'].map((key) => pm.variables.get(key) || '')",
          ")).toString();",
          "// access_token_for is kept: it records which settings were last used, so a later",
          "// tenant switch is still detected after a token has been dropped.",
          "const forgetToken = () => ['access_token', 'access_token_expires_at']",
          "  .forEach((key) => pm.collectionVariables.unset(key));",
          "const tokenUsableNow = () => pm.collectionVariables.get('access_token_for') === settingsHash",
          "  && Boolean(pm.collectionVariables.get('access_token'))",
          "  && Date.now() < Number(pm.collectionVariables.get('access_token_expires_at') || 0);",
          "// requestedAt is when the token request was sent. expires_in counts from when the",
          "// server generated the response, which is later, so this never overestimates.",
          "const saveToken = (body, requestedAt) => {",
          "  pm.collectionVariables.set('access_token', body.access_token);",
          "  pm.collectionVariables.set('access_token_expires_at', String(requestedAt + body.expires_in * 1000));",
          "  pm.collectionVariables.set('access_token_for', settingsHash);",
          "};",
          "// Settings changed since the cached token was minted: the sample ids belong to the",
          "// previous tenant, so drop them now, before any token request can succeed or fail.",
          "const cachedFor = pm.collectionVariables.get('access_token_for');",
          "if (cachedFor && cachedFor !== settingsHash) SAMPLE_VARS.forEach((key) => pm.collectionVariables.unset(key));",
          "if (pm.info.requestName !== 'Get an access token' && pm.info.requestName !== 'SMART configuration') {",
          "  const expiresAt = Number(pm.collectionVariables.get('access_token_expires_at') || 0);",
          "  if (!(tokenUsableNow() && Date.now() < expiresAt - 30000)) {",
          "    const requestedAt = Date.now();",
          "    pm.sendRequest({",
          "      url: pm.variables.replaceIn('{{base_url}}/oauth/token'),",
          "      method: 'POST',",
          "      header: { 'Content-Type': 'application/x-www-form-urlencoded' },",
          "      body: {",
          "        mode: 'urlencoded',",
          "        urlencoded: [",
          "          { key: 'grant_type', value: 'client_credentials' },",
          "          { key: 'client_id', value: pm.variables.get('client_id') },",
          "          { key: 'client_secret', value: pm.variables.get('client_secret') },",
          "          { key: 'scope', value: pm.variables.get('scope') },",
          "          { key: 'tenant_id', value: pm.variables.get('tenant_id') },",
          "          { key: 'aud', value: pm.variables.get('fhir_base') }",
          "        ]",
          "      }",
          "    }, (err, res) => {",
          "      if (!err && res.code === 200) {",
          "        saveToken(res.json(), requestedAt);",
          "        return;",
          "      }",
          "      console.error('Token request failed:', err ? String(err) : res.code + ' ' + res.text());",
          "      // A token that is still valid for these settings (checked now, after the",
          "      // refresh failed) keeps working until it expires. Otherwise the request is",
          "      // skipped, so the token error above is what you see rather than a 401 from the",
          "      // FHIR API.",
          "      if (!tokenUsableNow()) {",
          "        forgetToken();",
          "        pm.execution.skipRequest();",
          "      }",
          "    });",
          "  }",
          "}"
        ]
      }
    }
  ],
  "variable": [
    {
      "key": "access_token",
      "value": ""
    },
    {
      "key": "access_token_expires_at",
      "value": ""
    },
    {
      "key": "access_token_for",
      "value": ""
    },
    {
      "key": "token_requested_at",
      "value": ""
    },
    {
      "key": "patient_id",
      "value": ""
    },
    {
      "key": "innowell_patient_id",
      "value": ""
    },
    {
      "key": "patient_name",
      "value": ""
    },
    {
      "key": "practitioner_id",
      "value": ""
    },
    {
      "key": "practitioner_name",
      "value": ""
    },
    {
      "key": "since",
      "value": "2026-01-01"
    }
  ],
  "item": [
    {
      "name": "1. Token",
      "item": [
        {
          "name": "Get an access token",
          "event": [
            {
              "listen": "prerequest",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "// Recorded just before this request is sent; the test script uses it as the start",
                  "// of the token's lifetime.",
                  "pm.collectionVariables.set('token_requested_at', String(Date.now()));"
                ]
              }
            },
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "const cryptoJs = require('crypto-js');",
                  "const SAMPLE_VARS = ['patient_id', 'innowell_patient_id', 'patient_name', 'practitioner_id', 'practitioner_name'];",
                  "const settingsHash = cryptoJs.SHA256(JSON.stringify(",
                  "  ['base_url', 'fhir_base', 'client_id', 'client_secret', 'tenant_id', 'scope'].map((key) => pm.variables.get(key) || '')",
                  ")).toString();",
                  "// access_token_for is kept: it records which settings were last used, so a later",
                  "// tenant switch is still detected after a token has been dropped.",
                  "const forgetToken = () => ['access_token', 'access_token_expires_at']",
                  "  .forEach((key) => pm.collectionVariables.unset(key));",
                  "const tokenUsableNow = () => pm.collectionVariables.get('access_token_for') === settingsHash",
                  "  && Boolean(pm.collectionVariables.get('access_token'))",
                  "  && Date.now() < Number(pm.collectionVariables.get('access_token_expires_at') || 0);",
                  "// requestedAt is when the token request was sent. expires_in counts from when the",
                  "// server generated the response, which is later, so this never overestimates.",
                  "const saveToken = (body, requestedAt) => {",
                  "  pm.collectionVariables.set('access_token', body.access_token);",
                  "  pm.collectionVariables.set('access_token_expires_at', String(requestedAt + body.expires_in * 1000));",
                  "  pm.collectionVariables.set('access_token_for', settingsHash);",
                  "};",
                  "// Settings changed since the cached token was minted: the sample ids belong to the",
                  "// previous tenant, so drop them now, before any token request can succeed or fail.",
                  "const cachedFor = pm.collectionVariables.get('access_token_for');",
                  "if (cachedFor && cachedFor !== settingsHash) SAMPLE_VARS.forEach((key) => pm.collectionVariables.unset(key));",
                  "pm.test('Token issued', () => pm.response.to.have.status(200));",
                  "if (pm.response.code === 200) {",
                  "  saveToken(pm.response.json(), Number(pm.collectionVariables.get('token_requested_at')));",
                  "}",
                  "pm.collectionVariables.unset('token_requested_at');"
                ]
              }
            }
          ],
          "request": {
            "auth": {
              "type": "noauth"
            },
            "method": "POST",
            "header": [
              {
                "key": "Content-Type",
                "value": "application/x-www-form-urlencoded"
              }
            ],
            "body": {
              "mode": "urlencoded",
              "urlencoded": [
                {
                  "key": "grant_type",
                  "value": "client_credentials"
                },
                {
                  "key": "client_id",
                  "value": "{{client_id}}"
                },
                {
                  "key": "client_secret",
                  "value": "{{client_secret}}"
                },
                {
                  "key": "scope",
                  "value": "{{scope}}"
                },
                {
                  "key": "tenant_id",
                  "value": "{{tenant_id}}"
                },
                {
                  "key": "aud",
                  "value": "{{fhir_base}}",
                  "description": "Required: the FHIR API only accepts tokens issued for the FHIR base URL."
                }
              ]
            },
            "url": {
              "raw": "{{base_url}}/oauth/token",
              "host": [
                "{{base_url}}"
              ],
              "path": [
                "oauth",
                "token"
              ]
            },
            "description": "Mints a 300-second system token. Optional: the collection fetches one automatically."
          }
        },
        {
          "name": "SMART configuration",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Status 200', () => pm.response.to.have.status(200));"
                ]
              }
            }
          ],
          "request": {
            "auth": {
              "type": "noauth"
            },
            "method": "GET",
            "header": [],
            "url": {
              "raw": "{{base_url}}/.well-known/smart-configuration",
              "host": [
                "{{base_url}}"
              ],
              "path": [
                ".well-known",
                "smart-configuration"
              ]
            },
            "description": "Discovery document: token endpoint, grant types, supported scopes."
          }
        }
      ]
    },
    {
      "name": "2. Patients",
      "item": [
        {
          "name": "Search patients",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Status 200', () => pm.response.to.have.status(200));",
                  "const patients = (pm.response.json().entry || []).map((e) => e.resource);",
                  "const withClinician = patients.find((p) => p.generalPractitioner && p.generalPractitioner.length) || patients[0];",
                  "if (withClinician) {",
                  "  pm.collectionVariables.set('patient_id', withClinician.id);",
                  "  pm.collectionVariables.set('innowell_patient_id', (withClinician.identifier || [{}])[0].value || '');",
                  "  const name = ((withClinician.name || [{}])[0].text || '').split(' ')[0];",
                  "  if (name) pm.collectionVariables.set('patient_name', name);",
                  "  const gp = (withClinician.generalPractitioner || [])[0];",
                  "  if (gp) pm.collectionVariables.set('practitioner_id', gp.reference.replace('Practitioner/', ''));",
                  "}"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "Accept",
                "value": "application/fhir+json"
              }
            ],
            "url": {
              "raw": "{{fhir_base}}/Patient?_count=50",
              "host": [
                "{{fhir_base}}"
              ],
              "path": [
                "Patient"
              ],
              "query": [
                {
                  "key": "_count",
                  "value": "50"
                }
              ]
            },
            "description": "First page of patients. Follow the Bundle's next link for more."
          }
        },
        {
          "name": "Count patients",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Status 200', () => pm.response.to.have.status(200));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "Accept",
                "value": "application/fhir+json"
              }
            ],
            "url": {
              "raw": "{{fhir_base}}/Patient?_summary=count",
              "host": [
                "{{fhir_base}}"
              ],
              "path": [
                "Patient"
              ],
              "query": [
                {
                  "key": "_summary",
                  "value": "count"
                }
              ]
            },
            "description": "Total only, no records."
          }
        },
        {
          "name": "Read patient by id",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Status 200', () => pm.response.to.have.status(200));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "Accept",
                "value": "application/fhir+json"
              }
            ],
            "url": {
              "raw": "{{fhir_base}}/Patient/{{patient_id}}",
              "host": [
                "{{fhir_base}}"
              ],
              "path": [
                "Patient",
                "{{patient_id}}"
              ]
            },
            "description": "One patient by FHIR id."
          }
        },
        {
          "name": "Find patient by Innowell id",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Status 200', () => pm.response.to.have.status(200));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "Accept",
                "value": "application/fhir+json"
              }
            ],
            "url": {
              "raw": "{{fhir_base}}/Patient?identifier=https://innowell.org/fhir/sid/role%7C{{innowell_patient_id}}",
              "host": [
                "{{fhir_base}}"
              ],
              "path": [
                "Patient"
              ],
              "query": [
                {
                  "key": "identifier",
                  "value": "https://innowell.org/fhir/sid/role%7C{{innowell_patient_id}}"
                }
              ]
            },
            "description": "Looks a patient up by their Innowell id."
          }
        },
        {
          "name": "Search patients by name",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Status 200', () => pm.response.to.have.status(200));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "Accept",
                "value": "application/fhir+json"
              }
            ],
            "url": {
              "raw": "{{fhir_base}}/Patient?name={{patient_name}}",
              "host": [
                "{{fhir_base}}"
              ],
              "path": [
                "Patient"
              ],
              "query": [
                {
                  "key": "name",
                  "value": "{{patient_name}}"
                }
              ]
            },
            "description": "Matches the free-text name."
          }
        },
        {
          "name": "Patients assigned to a clinician",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Status 200', () => pm.response.to.have.status(200));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "Accept",
                "value": "application/fhir+json"
              }
            ],
            "url": {
              "raw": "{{fhir_base}}/Patient?general-practitioner=Practitioner/{{practitioner_id}}",
              "host": [
                "{{fhir_base}}"
              ],
              "path": [
                "Patient"
              ],
              "query": [
                {
                  "key": "general-practitioner",
                  "value": "Practitioner/{{practitioner_id}}"
                }
              ]
            },
            "description": "Patients whose generalPractitioner includes this clinician."
          }
        },
        {
          "name": "Patients with their clinicians",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Status 200', () => pm.response.to.have.status(200));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "Accept",
                "value": "application/fhir+json"
              }
            ],
            "url": {
              "raw": "{{fhir_base}}/Patient?_include=Patient:general-practitioner&_count=10",
              "host": [
                "{{fhir_base}}"
              ],
              "path": [
                "Patient"
              ],
              "query": [
                {
                  "key": "_include",
                  "value": "Patient:general-practitioner"
                },
                {
                  "key": "_count",
                  "value": "10"
                }
              ]
            },
            "description": "Patients plus the Practitioners they reference, in one Bundle."
          }
        },
        {
          "name": "Patients changed since",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Status 200', () => pm.response.to.have.status(200));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "Accept",
                "value": "application/fhir+json"
              }
            ],
            "url": {
              "raw": "{{fhir_base}}/Patient?_lastUpdated=gt{{since}}",
              "host": [
                "{{fhir_base}}"
              ],
              "path": [
                "Patient"
              ],
              "query": [
                {
                  "key": "_lastUpdated",
                  "value": "gt{{since}}"
                }
              ]
            },
            "description": "For incremental sync: records changed after the date in the since variable."
          }
        }
      ]
    },
    {
      "name": "3. Clinicians (Practitioners)",
      "item": [
        {
          "name": "Search practitioners",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Status 200', () => pm.response.to.have.status(200));",
                  "const practitioners = (pm.response.json().entry || []).map((e) => e.resource);",
                  "const named = practitioners.find((p) => p.name && p.name[0] && p.name[0].text);",
                  "if (named) pm.collectionVariables.set('practitioner_name', named.name[0].text.split(' ')[0]);",
                  "if (!pm.collectionVariables.get('practitioner_id') && practitioners[0]) {",
                  "  pm.collectionVariables.set('practitioner_id', practitioners[0].id);",
                  "}"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "Accept",
                "value": "application/fhir+json"
              }
            ],
            "url": {
              "raw": "{{fhir_base}}/Practitioner?_count=50",
              "host": [
                "{{fhir_base}}"
              ],
              "path": [
                "Practitioner"
              ],
              "query": [
                {
                  "key": "_count",
                  "value": "50"
                }
              ]
            },
            "description": "First page of clinicians."
          }
        },
        {
          "name": "Read practitioner by id",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Status 200', () => pm.response.to.have.status(200));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "Accept",
                "value": "application/fhir+json"
              }
            ],
            "url": {
              "raw": "{{fhir_base}}/Practitioner/{{practitioner_id}}",
              "host": [
                "{{fhir_base}}"
              ],
              "path": [
                "Practitioner",
                "{{practitioner_id}}"
              ]
            },
            "description": "One clinician by FHIR id."
          }
        },
        {
          "name": "Search practitioners by name",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Status 200', () => pm.response.to.have.status(200));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "Accept",
                "value": "application/fhir+json"
              }
            ],
            "url": {
              "raw": "{{fhir_base}}/Practitioner?name={{practitioner_name}}",
              "host": [
                "{{fhir_base}}"
              ],
              "path": [
                "Practitioner"
              ],
              "query": [
                {
                  "key": "name",
                  "value": "{{practitioner_name}}"
                }
              ]
            },
            "description": "Matches the free-text name."
          }
        },
        {
          "name": "Active practitioners",
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "pm.test('Status 200', () => pm.response.to.have.status(200));"
                ]
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "Accept",
                "value": "application/fhir+json"
              }
            ],
            "url": {
              "raw": "{{fhir_base}}/Practitioner?active=true",
              "host": [
                "{{fhir_base}}"
              ],
              "path": [
                "Practitioner"
              ],
              "query": [
                {
                  "key": "active",
                  "value": "true"
                }
              ]
            },
            "description": "Clinicians who currently have access."
          }
        }
      ]
    }
  ]
}
