---
title: Saving a card
description: Save a card to a connected patient's Nurish'd account with Stripe.js, so a visit can be booked for them.
section: Care
order: 2
---

# Saving a card

You will save a card to a patient's Nurish'd account, confirm it with Stripe.js on your own page, and list the saved cards.

## Why a card comes first

A visit charges nothing when it is booked. The visit, a late-cancellation fee and a no-show fee are charged later to the patient's default saved card. A patient made through a company has no card. So a patient with no default card cannot be booked: booking answers `409 payment_method_required` and books nothing.

The order of calls is:

1. Connect the patient. See [Connecting a patient](/docs/care/connecting-a-patient).
2. Save a card. This page.
3. Book. See [Booking a visit](/docs/care/booking-a-visit).

Every route on this page needs the `patient:write` scope, an `active` connection and a connection that allows `care.bookings`. `patient:write` needs a signed BAA. See [BAA handling](/docs/care/baa).

> **Warning.** The card is saved to the patient's Nurish'd account and may be charged for visits, late-cancellation fees and no-show fees. Tell the patient so before they type the card in.

Nurish'd Connect keeps no record of card numbers. The patient types the card into Stripe's own form on your page. The number never reaches your server or Nurish'd's.

## Step 1: start the setup

`POST /care/v1/patient/{patient_id}/payment-method/setup/create/document` starts saving a card. It takes no body. `patient_id` is the id from the create-patient answer.

```bash
curl -X POST https://api.dev.nurishd.ca/care/v1/patient/6f1c0d2e-0000-4000-8000-000000000101/payment-method/setup/create/document \
  -H "X-Partner-API-Key: $NURISHD_API_KEY"
```

```javascript
const patientId = '6f1c0d2e-0000-4000-8000-000000000101';
const res = await fetch(
	`https://api.dev.nurishd.ca/care/v1/patient/${patientId}/payment-method/setup/create/document`,
	{
		method: 'POST',
		headers: { 'X-Partner-API-Key': process.env.NURISHD_API_KEY },
	},
);
const setup = await res.json();
```

```python
import os
import requests

patient_id = "6f1c0d2e-0000-4000-8000-000000000101"
res = requests.post(
    f"https://api.dev.nurishd.ca/care/v1/patient/{patient_id}/payment-method/setup/create/document",
    headers={"X-Partner-API-Key": os.environ["NURISHD_API_KEY"]},
)
setup = res.json()
```

```json
{
	"id": "seti_example",
	"client_secret": "seti_example_secret_example",
	"status": "requires_payment_method",
	"stripe_publishable_key": "pk_test_example"
}
```

| Field                    | Meaning                                                                                   |
| ------------------------ | ----------------------------------------------------------------------------------------- |
| `id`                     | The id of the setup. You send it to the confirm route in step 3.                          |
| `client_secret`          | The secret the card form confirms the card with. Give it to Stripe.js only. Never log it. |
| `status`                 | Where the setup is. It is `requires_payment_method` until a card is typed in.             |
| `stripe_publishable_key` | The Stripe publishable key to load Stripe.js with. `null` when none is configured.        |

Calling start again returns the same open setup while it is less than a day old.

`stripe_publishable_key` can be `null` in an environment where Stripe is not configured yet. Your page cannot load Stripe.js then. Show an error and try again later.

## Step 2: confirm the card in the browser

Send `stripe_publishable_key` and `client_secret` to your page. Never send your API key. The code below runs in the browser, on your company's page, not on your server. It uses [Stripe.js](https://docs.stripe.com/js) and a card element that you mounted.

```javascript
// Runs in the browser, on your page. Not server code.
const stripe = Stripe(publishableKey);
const { error, setupIntent } = await stripe.confirmCardSetup(clientSecret, {
	payment_method: { card: cardElement },
});
if (error) {
	// Show error.message to the patient. Do not call the confirm route.
} else {
	// The card form accepted the card. Tell your server to run step 3.
}
```

`confirmCardSetup` is the Stripe.js call for a setup secret like the one the answer carries, which starts with `seti_`. When Stripe.js reports success, call the confirm route right away. It does not wait for any notification.

## Step 3: record the card

`PUT /care/v1/patient/{patient_id}/payment-method/setup/{setup_id}/confirm/document` checks the setup and records the card on the patient's account. It takes no body. `setup_id` is the `id` from step 1.

```bash
curl -X PUT https://api.dev.nurishd.ca/care/v1/patient/6f1c0d2e-0000-4000-8000-000000000101/payment-method/setup/seti_example/confirm/document \
  -H "X-Partner-API-Key: $NURISHD_API_KEY"
```

```javascript
const res = await fetch(
	`https://api.dev.nurishd.ca/care/v1/patient/${patientId}/payment-method/setup/${setup.id}/confirm/document`,
	{
		method: 'PUT',
		headers: { 'X-Partner-API-Key': process.env.NURISHD_API_KEY },
	},
);
const card = await res.json();
```

```python
setup_id = setup["id"]
res = requests.put(
    f"https://api.dev.nurishd.ca/care/v1/patient/{patient_id}/payment-method/setup/{setup_id}/confirm/document",
    headers={"X-Partner-API-Key": os.environ["NURISHD_API_KEY"]},
)
card = res.json()
```

```json
{
	"id": "pm_example",
	"card": {
		"brand": "visa",
		"last4": "4242",
		"exp_month": 12,
		"exp_year": 2030
	},
	"is_default": true
}
```

Calling confirm again for the same setup is safe.

### Which card is the default

When the patient had no default card, the saved card becomes the default, and the answer says `is_default: true`. A patient who already has a default card keeps it, and the answer says `is_default: false`. Visits and fees are charged to the default card.

## List saved cards

`GET /care/v1/patient/{patient_id}/payment-method/query/documents` returns the cards saved to the patient's account, at most 10, in one page. A patient with no card gets an empty `data` list. Each card has `id`, `card` (`brand`, `exp_month`, `exp_year` and `last4`) and `is_default`, which is `true` for the card visits and fees are charged to.

```bash
curl https://api.dev.nurishd.ca/care/v1/patient/6f1c0d2e-0000-4000-8000-000000000101/payment-method/query/documents \
  -H "X-Partner-API-Key: $NURISHD_API_KEY"
```

```javascript
const res = await fetch(
	`https://api.dev.nurishd.ca/care/v1/patient/${patientId}/payment-method/query/documents`,
	{ headers: { 'X-Partner-API-Key': process.env.NURISHD_API_KEY } },
);
const cards = await res.json();
```

```python
res = requests.get(
    f"https://api.dev.nurishd.ca/care/v1/patient/{patient_id}/payment-method/query/documents",
    headers={"X-Partner-API-Key": os.environ["NURISHD_API_KEY"]},
)
cards = res.json()
```

```json
{
	"data": [
		{
			"id": "pm_example",
			"card": {
				"brand": "visa",
				"last4": "4242",
				"exp_month": 12,
				"exp_year": 2030
			},
			"is_default": true
		}
	],
	"total": 1,
	"total_pages": 1,
	"page": 1,
	"size": 10,
	"has_next": false,
	"has_previous": false
}
```

The patient can also add, remove and choose cards in their own Nurish'd account, so the list can change without you. Read it before you book if you need to know.

## Errors

See [Errors](/docs/concepts/errors) for the shape. All three routes share the connection errors.

| Status and code                       | What it means for you                                                                               |
| ------------------------------------- | --------------------------------------------------------------------------------------------------- |
| `404 not_found`                       | The patient has no care connection with your company, or the setup is not this patient's.           |
| `403 connection_scope_not_granted`    | The patient did not allow bookings. Create the patient again with a key that holds `booking:write`. |
| `403 scope_not_granted`               | The key lacks `patient:write`. See [Scopes](/docs/concepts/scopes).                                 |
| `409 connection_not_active`           | The connection is `pending` or was revoked. Read the connection and wait for approval.              |
| `409 payment_method_setup_incomplete` | Confirm only: the card form has not confirmed the card yet. Finish step 2, then call confirm again. |

## What the Sandbox does

> **Note.** Use Stripe test cards with a Sandbox key. Patients in the Sandbox are fake. See [Environments](/docs/get-started/environments).

## Next

- [Booking a visit](/docs/care/booking-a-visit): book once a card is saved.
- [Connecting a patient](/docs/care/connecting-a-patient): the step before this one.
- [Scopes](/docs/concepts/scopes): what `patient:write` allows.
