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

# Ordering

> Learn the ordering concepts including markers, lab tests, and the various order_set field combinations for placing lab test orders.

# Concepts

Junction has a series of concepts to grasp regarding ordering:

* [Lab Tests](/lab/workflow/create-test)
* [Lifecycle of an Order](/lab/workflow/lab-test-lifecycle)
* [Order Transactions](/lab/workflow/order-transactions)
* [AoE](/lab/workflow/aoe)
* [Registrable Kits](/lab/workflow/order-registrable-testkit)
* [Scheduled Orders](/lab/workflow/scheduled-orders)

In this document, we will focus on orderable panels/biomarkers.

### Markers

[*Markers*](/api-reference/lab-testing/biomarkers) are individual, orderable tests, at the lab level. So, for example, at *Labcorp* you can order a `Lipid Panel` test and a `Vitamin D` test, a `panel` and a `biomarker` respectively. At Junction, these are both referred to as **markers**.

### Lab Tests

[*Lab Tests*](/api-reference/lab-testing/post-test) are a collection of markers, or a preset combination of markers, that you can order. Using the example above, you can create a `Labcorp Lipid Panel and Vitamin D` lab test. You can then place orders at Junction using this preset. This is useful for situations where you repeatedly want to order the same markers.

# Ordering

When placing an [Order](/api-reference/lab-testing/create-order), you will see an `order_set` field. This is what defines what markers will be ordered.

There are multiple combinations allowed with this field, so let's explore all of them.

### Ordering a *Lab Test*

When ordering from **one** previously created *Lab Test*, the `order_set` field should be populated as follows:

<CodeGroup>
  ```python Python theme={null}
  from junction import Junction, OrderSetRequest
  from junction.environment import JunctionEnvironment

  client = Junction(
      api_key="YOUR_API_KEY",
      environment=JunctionEnvironment.SANDBOX,
  )

  client.lab_tests.create_order(
      order_set=OrderSetRequest(
          lab_test_ids=["<lab_test_id>"],
      ),
      # ...
  )
  ```
</CodeGroup>

### Ordering from multiple *Lab Tests*

You may want to combine two or more existing Lab Tests without creating a new one. This is possible by doing:

<CodeGroup>
  ```python Python theme={null}
  from junction import Junction, OrderSetRequest
  from junction.environment import JunctionEnvironment

  client = Junction(
      api_key="YOUR_API_KEY",
      environment=JunctionEnvironment.SANDBOX,
  )

  client.lab_tests.create_order(
      order_set=OrderSetRequest(
          lab_test_ids=["<lab_test_id_1>", "<lab_test_id_2>"],
      ),
      # ...
  )
  ```
</CodeGroup>

### Ordering without a *Lab Test*

This is what Junction calls `à la carte` ordering. You may want to order from the marker compendium without creating a preset.

<Note>
  This is a team-level configuration that must be requested from Junction. Not all markers can be ordered in this manner.
</Note>

It is possible to order `à la carte` using Junction `marker_ids` or the lab's `provider_ids`.

<CodeGroup>
  ```python Python theme={null}
  from junction import AddOnOrder, Junction, LabTestCollectionMethod, OrderSetRequest
  from junction.environment import JunctionEnvironment

  client = Junction(
      api_key="YOUR_API_KEY",
      environment=JunctionEnvironment.SANDBOX,
  )

  client.lab_tests.create_order(
      order_set=OrderSetRequest(
          add_on=AddOnOrder(
              provider_ids=["322022"],
              # marker_ids=[1],
          ),
      ),
      collection_method=LabTestCollectionMethod.WALK_IN_TEST,
      # ...
  )
  ```
</CodeGroup>

### Order *Lab Tests* with extra *Markers*

You may also add extra **markers** to an order. For example, you want to order the `Labcorp Lipid Panel and Vitamin D` but for this particular patient, you also want to order a `CBC Panel`.

<CodeGroup>
  ```python Python theme={null}
  from junction import AddOnOrder, Junction, LabTestCollectionMethod, OrderSetRequest
  from junction.environment import JunctionEnvironment

  client = Junction(
      api_key="YOUR_API_KEY",
      environment=JunctionEnvironment.SANDBOX,
  )

  client.lab_tests.create_order(
      order_set=OrderSetRequest(
          lab_test_ids=["<lab_test_id>"],
          add_on=AddOnOrder(
              provider_ids=["322022"],
              # marker_ids=[1],
          ),
      ),
      collection_method=LabTestCollectionMethod.WALK_IN_TEST,
      # ...
  )
  ```
</CodeGroup>

<Note>
  When supplying the `add_on` field, it is always required to provide the `collection_method` field.
</Note>

## Collection Method

As further explored in the documentation, Junction also has the concept of *Collection Method*. Junction currently supports four methods: [`At Home Phlebotomy`](/lab/at-home-phlebotomy/overview), [`Walk In`](/lab/walk-in/overview), [`Testkits`](/lab/testkits/overview), and [`On-Site Collection`](/lab/on-site-collection/overview).
When ordering, you must select one of these, either at lab test creation, or at ordering time.

<CodeGroup>
  ```python Python theme={null}
  from junction import Junction, LabTestCollectionMethod, OrderSetRequest
  from junction.environment import JunctionEnvironment

  client = Junction(
      api_key="YOUR_API_KEY",
      environment=JunctionEnvironment.SANDBOX,
  )

  client.lab_tests.create_order(
      order_set=OrderSetRequest(
          lab_test_ids=["<lab_test_id>"],
      ),
      collection_method=LabTestCollectionMethod.WALK_IN_TEST,
      # ...
  )
  ```
</CodeGroup>

## À La Carte Markers

As mentioned above, not all `markers` are `à la carte` orderable. You can find which ones are orderable via the [GET /v3/lab\_tests/markers](/api-reference/lab-testing/biomarkers).

<CodeGroup>
  ```python Python theme={null}
  from junction import Junction
  from junction.environment import JunctionEnvironment

  client = Junction(
      api_key="YOUR_API_KEY",
      environment=JunctionEnvironment.SANDBOX,
  )

  markers = client.lab_tests.get_markers(
      name="322022",
      a_la_carte_enabled=True,
  )

  if not all(m.a_la_carte_enabled for m in markers.markers):
      raise Exception("Markers not a_la_carte_enabled")
  ```
</CodeGroup>

## Error Cases

With the existence of various allowed combinations with the `order_set` field, there are many validations that are done server side. Here are some of the errors you can expect to encounter:

### 400 Bad Request

1. `collection_method must be set if add_on is set`: When the `add_on` field is supplied, you must provide the `collection_method`.
2. `marker_ids or provider_ids must be set in add_on`: One of `marker_ids` or `provider_ids` must be set, if the `add_on` field is provided.
3. `cannot set both marker_ids and provider_ids in add_on`: Similarly, only one of these two fields can be provided.
4. `cannot order lab_tests from multiple labs`: You can only order multiple lab tests from the same lab.
5. `cannot order lab tests with multiple collection methods`: You must supply a `collection_method` if ordering multiple lab tests with multiple collection methods.
6. `Lab <lab name> does not allow gender <gender value>`: The associated lab restricts which patient genders it accepts. This restriction applies to the entire lab, not to individual lab tests.
