> ## Documentation Index
> Fetch the complete documentation index at: https://docs.metriport.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Start Document Query (Legacy)

> Triggers a document query for the specified patient across HIEs.

<Warning>
  **Legacy Endpoint:** This endpoint is deprecated. We recommend using the [Network Query](/medical-api/api-reference/network/start-network-query) endpoint instead, which can query all available health data networks (HIEs, pharmacies, and laboratories) in a single call, and returns a direct url to the updated patient record.
</Warning>

When executed, this endpoint triggers an asynchronous document query across HIEs. This is a two step process where the documents will
first be downloaded from the respective HIE and, if they are C-CDA/XML, then converted to FHIR.

Each process (download, conversion) will contain its own `total` and `status` as well as the count for `successful` operations and `errors`.

When the asynchronous document query finishes, it stores new/updated document references for future requests and
updates the status of download to `completed`. Meanwhile, in the background, files will be converted and the convert count will be
incremented. Once all documents have been converted it too will be marked as `completed`.

If there's no document to be converted, the total will be set to zero and the status to `completed`.

Once each process completes, a webhook request will be sent to [your configured URL](/medical-api/api-reference/settings/post-settings)
containing the available data. **Note:** the webhooks will only contain updates for new data fetched in the current document query.

Webhook message types - see [the respective section on the webhooks page](/medical-api/handling-data/webhooks#patient-document-data)
for more details:

* `medical.document-download`: contains the newly downloaded documents for the patient;
* `medical.document-conversion`: result of converting the newly downloaded C-CDA documents into FHIR.

<Warning>
  If you were to trigger this endpoint again while the query is still processing, you will get a
  response that reflects the current query progress. So essentially, only a single document query
  will be running for a patient at any given time.
</Warning>

## Query Params

<ParamField query="patientId" type="string" required>
  The ID of the Patient for which to list available Documents.
</ParamField>

<ParamField query="facilityId" type="string" required>
  The ID of the Facility where the patient is receiving care.
</ParamField>

<ParamField query="pharmacies" type="boolean">
  When set to `true`, the document query will also request detailed medication history nationally across pharmacies and PBMs,
  including advanced insights like fill data - allowing you to know whether the patient picked up their medications, for example.

  <Info>
    This will retrieve up to one year of medication history.
  </Info>
</ParamField>

## Body

<Snippet file="webhook-request-metadata.mdx" />

<Snippet file="webhook-metadata-post-example.mdx" />

## Response

<Snippet file="document-progress-download.mdx" />

<Snippet file="request-id.mdx" />

```json theme={null}
{
  "download": {
    "status": "processing"
  },
  "requestId": "018a80c4-292a-7486-a223-6dcbc636c44c"
}
```

<ResponseExample>
  ```javascript Metriport SDK theme={null}
  import { MetriportMedicalApi } from "@metriport/api-sdk";

  const metriport = new MetriportMedicalApi("YOUR_API_KEY");

  const status = await metriport.startDocumentQuery(
  "018a80c4-292a-7486-a1234-76yuhe23yu14", // Patient
  "018a80c4-292a-7486-a1234-9uiu76yhe234", // Facility
  {
      youCan: "putAny",
      stringKeyValue: "pairsHere",
    }
  );
  ```
</ResponseExample>

## Rate Limits

See [limits and throttling](/medical-api/more-info/limits#rate-limits)
