Skip to content

Test and Live mode, and IDs

How the API key sets the mode, and the prefixes of public IDs.

Updated View as Markdown

Test and Live mode

Affinity has two modes:

  • Test is for development. Use synthetic patients only.
  • Live sends real prescriptions to pharmacies.

Your API key sets the mode. A key starting with sk_test_ acts in Test. A key starting with sk_live_ acts in Live. The API address is the same for both.

Practices are shared across Test and Live. Create each practice once. Both keys read the same practice ID, externalId, name and contact details. The externalId is your own ID for the practice. Test and Live share one value, so send the same externalId with both keys. A Test key can change the externalId. The change also applies in Live. An externalId is unique among the platform’s active practices across both modes.

A create or update with an already used externalId returns 409 practice_already_exists. The response gives the existing practice ID in data.practiceId. Name and address do not identify a practice.

Only a Live key can change liveEnabled; a Test key that changes it gets 403 live_key_required. After a practice has Live access, its name, phone and address also require a Live key to change. A Test key gets 403 live_mode_required, even for an unchanged value. When a practice has no Live access, a Test key can change its name, phone and address. This change also updates the pharmacy’s saved record of the practice, including a record used for Live orders. Existing orders keep the delivery address they were signed with.

Each mode has its own records:

  • Orders, patients and webhook endpoints belong to one mode. A Test key cannot read a Live order. A Live order ID with a Test key returns 404.
  • Webhook events of a mode go to the endpoints of that mode only.
  • Medication and shipping option IDs are shared across modes. Read the catalog with the key that you use to order. An old Test shipping ID returns 422 invalid_order in Live.
  • Test is always available for every practice. liveEnabled shows whether the practice can place Live orders.

An organization that you create starts with Live access. An organization that Affinity invites you to also starts with Live access. Live orders need Live access on the practice and its platform, when it has one.

A Live order needs a payment method on file, or account credit that pays the full order. Without one, the order gets 409 payment_method_required. An admin adds a card or a bank account on the Billing page of the Affinity dashboard (Settings → Billing). Read Go Live. If Affinity set up your organization to pay by invoice, a Live order needs no payment method. It goes on your invoice. If a platform or an independent practice does not have Live access, an admin adds a payment method in Settings → Billing and clicks Turn on Live. For a platform-managed practice, read Practices.

When Live is turned off, new Live orders and Live API keys get 403 mode_not_enabled. Only new Live orders stop. Orders already sent to the pharmacy keep going. To ask for a cancel, send POST /v1/orders/{orderId}/cancel or open the order in the dashboard. A platform’s switch overrides its practices’ Live settings without clearing them. Practices with Live enabled regain access when the platform does. Existing keys and sessions keep their permissions to read records, cancel orders and manage webhooks. Billing, webhook delivery, and listing or deleting keys continue. The dashboard keeps Live available when it has saved Live records.

In the dashboard, switching Test/Live reloads the page in the same organization. Medication and pharmacy detail pages stay open with the same ID and search. If the item is unavailable in the destination mode, the page shows a not-found message and a link to the list. Order and patient detail pages open their lists. New order stays open, with the other mode’s own draft.

Go Live

  1. Make your integration work in Test, including webhooks.
  2. Add a payment method in Settings → Billing. If Live access is off, click Turn on Live on the same page. A practice under a platform asks its platform instead.
  3. Create a Live API key in Settings → API on the Live tab. Before Live is on, this returns 403 mode_not_enabled.
  4. A platform turns on Live access for each practice it sends Live orders for: a practice it creates starts in Test. With the Live API key, send "liveEnabled": true on create or with PATCH /v1/practices/{practiceId}.
  5. Create a Live webhook endpoint with the Live API key. Send the endpoint request with that key. The key sets the mode of the endpoint, so a Test key cannot create a Live endpoint. Store the signing secret from the response. A Live endpoint gets a new signing secret. You can also add the endpoint in Settings → API on the Live tab, in Webhooks.
  6. Change the API key and the webhook secrets in your server configuration together. Then send the first Live order. The Live endpoint gets the events of that order.

Billing

Affinity charges each Live order when it sends the order to the pharmacy. The charge uses your account credit first, then the payment method on file. Each order has its own invoice in Settings → Billing. Affinity never bills Test orders.

Some organizations get one invoice for the Live orders that shipped, which Affinity sends to them. When a card or bank account is on file, Affinity charges it. The rules below apply to that invoice.

The platform pays for its practices’ Live orders. An independent practice pays for its own Live orders. Billing is managed in the platform’s workspace for a platform-managed practice.

Affinity bills the full order total when the first package ships. Later packages do not add a second charge. An invoice lists each item once, with its total quantity and unit price, for all the orders on it. It does not list each order. To get the orders on an invoice, contact Affinity. If the pharmacy ships an order after cancellation, the order is billed because it shipped. You receive the shipment status webhook. Credits in Stripe must leave the draft total at zero or above.

How Test orders behave

A Test order follows the same rules as a Live order. It has the same validation, statuses and webhook events. These things are different:

  • A Test order goes to a test system for its pharmacy, not to the real pharmacy. No medication is made and no package ships. Both modes return the same prescriptions[].pharmacy.name. Check the order’s mode to identify Test or Live.
  • A Test order sends the same events, in the same order: order.submitted first, then the statuses that the test system reports, such as order.processing and order.shipped. Write your receiver for any status order, as for a Live order.
  • Only PerfectRx Pharmacy Test orders ship and deliver on their own. Read Test progress by pharmacy.
  • Each Test shipment has a carrier and a test tracking number that belongs to that order only.
  • A cancelled Test order does not advance.
  • These times come from the test system. They do not show how long a Live pharmacy takes. Read the order between changes to verify each status and its shipment fields.
  • A Live pharmacy can skip a status or never report delivered. A delivered event in Test does not show that the Live pharmacy reports delivery.
  • Affinity does not bill Test orders and starts no carrier tracking for them.
  • Test endpoints and Live endpoints are separate. A Test order sends events to Test endpoints only.

Test offers each pharmacy’s Live catalog with the same medication IDs, names and prices. Shipping option IDs are shared across Test and Live. Old Test shipping IDs remain valid in Test.

Test progress by pharmacy

A PerfectRx Pharmacy Test order with no outcome trigger finishes on its own:

  • It reaches processing when the test pharmacy accepts it.
  • The test pharmacy fills the order about 2 minutes after it receives it. After that, cancellation returns 409 cancel_refused. The order still shows processing until it ships.
  • The test pharmacy ships the order about 10 minutes after it accepts the order, and delivers it about 30 minutes after it accepts the order. Each change sends the matching webhook after Affinity reads the pharmacy update. You do not need to contact support to advance a Test order.

A Test order to any other pharmacy does not finish on its own:

  • It stays submitted, or it moves to processing when the test pharmacy receives it.
  • It does not move to shipped or delivered.
  • Test outcomes do not apply to it.

Test outcomes

Test outcomes apply to PerfectRx Pharmacy Test orders only. Use these case-sensitive prefixes at the start of each prescriptions[].notes value in POST /v1/orders. Add any suffix, such as your test run ID. Use the same prefix on every prescription in the order.

Notes value Result and events
"test_fail_checkout" Create returns submitted. After Affinity sends the order, the test pharmacy refuses it. The order becomes failed with a failureReason. You get order.submitted, then order.failed, usually within seconds.
"test_cancel_refused_checkout" You get order.submitted, then order.processing, usually within a minute. Wait for processing, then cancel the order. The pharmacy refuses. An immediate answer is 409 cancel_refused. If the request first returns 202, wait for the refusal. In both cases, cancelRequestedAt clears to null and you get order.updated. The order keeps progressing to order.shipped at about 10 minutes and order.delivered at about 30 minutes.
"test_split_shipment_checkout" The test pharmacy sends two packages with distinct tracking numbers. The first ships at about 10 minutes and delivers at 30. You get order.shipped, then order.delivered with one package. The second ships at about 2 hours and delivers 20 minutes later. It comes as order.updated with two packages in shipments, and the order stays delivered. Live PerfectRx orders can do this too: what delivered means.
Omit notes, or use any other value The order follows the automatic Test progress.

For example, add this field to each prescription in the send-an-order example:

{ "notes": "test_fail_checkout" }

"test_fail_", "test_cancel_refused_" and "test_split_shipment_" are the prefixes. The word checkout is only an example suffix. externalOrderId does not select an outcome because Affinity does not send it to PerfectRx. Other notes keep their usual meaning. No extra API key, support request or control endpoint is needed. After a simulated refusal, the order and each prescription have a failureReason. The failureReason is the same text as a real refusal. It tells a person to contact Affinity support. You do not need to contact Affinity support for a simulated refusal.

The times start when the test pharmacy receives the order. Background jobs and webhook delivery can add delay. Webhook retries can arrive later. Read the order to confirm its current status. A refused cancel does not send order.cancelled.

These values select outcomes in the PerfectRx test pharmacy wherever Affinity uses it. Staging Live also uses the test pharmacy, but a refused Live submission is held for Affinity staff. Production Live sends notes to the real pharmacy and does not simulate these outcomes. Use synthetic patients and a Test key for these tests.

The test pharmacy keeps the Test patients of each practice separate. In one practice, a patient with the same name and date of birth as an earlier patient, but with a different phone and a different email, fails patient matching. The order then fails before the test pharmacy reads the notes value. Use a different test patient for each scenario.

IDs

Each public ID has a prefix that names its type. Send IDs as Affinity returns them. Do not change or build them.

PrefixType
plat_ Platform
prac_ Practice
pharm_ Pharmacy
user_ User
mbr_ Member
pat_ Patient
key_ API key
med_ Medication
shp_ Shipping option
ord_ Order
rx_ Prescription
evt_ Webhook event
whe_ Webhook endpoint
fbk_ Feedback

A valid ID for another resource type in the endpoint path returns 404 not_found. For example, GET /v1/orders/pat_01j9z3k4m5n6p7q8r9s0t1v2w8 returns this error with an issues entry for orderId. A malformed ID, or a wrong resource type in a body or query field, returns 400 invalid_request. A record of another organization or mode returns 404 not_found.

Your own IDs go in externalOrderId and patient.externalId. Affinity returns externalOrderId in orders and webhook events.

Navigation

Send feedback

Don't include patient information.

Type to search…

↑↓ navigate ↵ select Esc close