---
title: "Create Patient"
description: "Create or upsert a patient record before booking appointments, writing notes, or attaching documents."
canonical_url: "http://localhost/api/quickstart/create-patient"
---
# Create Patient
> Create or upsert a patient record before booking appointments, writing notes, or attaching documents.
Use this guide to create or upsert a patient before booking appointments, writing medical notes, or attaching documents.
This guide requires `care_unit_id` from step 1 in [Show Doctor's Availability](http://localhost/api/quickstart/show-availability) before creating or upserting patients.
## Flow Overview

## Code Example
```python
import requests
CLIENT_ID = "your-client-id"
CLIENT_SECRET = "your-client-secret"
BASE_URL = "https://api.leyr.io/api"
EMR = "webdoc"
headers = {
"x-leyr-client-id": CLIENT_ID,
"x-leyr-client-secret": CLIENT_SECRET,
"Content-Type": "application/json"
}
care_unit_id = "L0_1"
# 1. Prepare patient payload
patient_payload = {
"first_name": "Tolvan",
"last_name": "Tolvansson",
"external_id": "191212121212",
"phones": ["+46001234567"],
"emails": ["Tolvan.Tolvansson@example.com"]
}
# 2. Create or upsert the patient
response = requests.post(
f"{BASE_URL}/emrs/{EMR}/care-units/{care_unit_id}/patients",
headers=headers,
json=patient_payload
)
patient = response.json()
# 3. Read key values from the response
print("✓ Patient created or updated")
print(f"Leyr ID: {patient['id']}")
print(f"EHR ID: {patient.get('emr_id')}")
```
```javascript
const CLIENT_ID = "your-client-id";
const CLIENT_SECRET = "your-client-secret";
const BASE_URL = "https://api.leyr.io/api";
const EMR = "webdoc";
const headers = {
"x-leyr-client-id": CLIENT_ID,
"x-leyr-client-secret": CLIENT_SECRET,
"Content-Type": "application/json"
};
const careUnitId = "L0_1";
// 1. Prepare patient payload
const patientPayload = {
first_name: "Tolvan",
last_name: "Tolvansson",
external_id: "191212121212",
phones: ["+46001234567"],
emails: ["Tolvan.Tolvansson@example.com"]
};
// 2. Create or upsert the patient
const response = await fetch(
`${BASE_URL}/emrs/${EMR}/care-units/${careUnitId}/patients`,
{
method: "POST",
headers,
body: JSON.stringify(patientPayload)
}
);
const patient = await response.json();
// 3. Read key values from the response
console.log("✓ Patient created or updated");
console.log(`Leyr ID: ${patient.id}`);
console.log(`EHR ID: ${patient.emr_id}`);
```
### Shared setup
```python
import requests
CLIENT_ID = "your-client-id"
CLIENT_SECRET = "your-client-secret"
BASE_URL = "https://api.leyr.io/api"
EMR = "webdoc"
headers = {
"x-leyr-client-id": CLIENT_ID,
"x-leyr-client-secret": CLIENT_SECRET,
"Content-Type": "application/json"
}
care_unit_id = "L0_1"
```
```javascript
const CLIENT_ID = "your-client-id";
const CLIENT_SECRET = "your-client-secret";
const BASE_URL = "https://api.leyr.io/api";
const EMR = "webdoc";
const headers = {
"x-leyr-client-id": CLIENT_ID,
"x-leyr-client-secret": CLIENT_SECRET,
"Content-Type": "application/json"
};
const careUnitId = "L0_1";
```
#### 1. Prepare patient payload
```python
patient_payload = {
"first_name": "Tolvan",
"last_name": "Tolvansson",
"external_id": "191212121212",
"phones": ["+46001234567"],
"emails": ["Tolvan.Tolvansson@example.com"]
}
```
```javascript
const patientPayload = {
first_name: "Tolvan",
last_name: "Tolvansson",
external_id: "191212121212",
phones: ["+46001234567"],
emails: ["Tolvan.Tolvansson@example.com"]
};
```
#### 2. Create or upsert the patient
```python
response = requests.post(
f"{BASE_URL}/emrs/{EMR}/care-units/{care_unit_id}/patients",
headers=headers,
json=patient_payload
)
patient = response.json()
```
```javascript
const response = await fetch(
`${BASE_URL}/emrs/${EMR}/care-units/${careUnitId}/patients`,
{
method: "POST",
headers,
body: JSON.stringify(patientPayload)
}
);
const patient = await response.json();
```
#### 3. Read key values from the response
```python
print("✓ Patient created or updated")
print(f"Leyr ID: {patient['id']}")
print(f"EHR ID: {patient.get('emr_id')}")
```
```javascript
console.log("✓ Patient created or updated");
console.log(`Leyr ID: ${patient.id}`);
console.log(`EHR ID: ${patient.emr_id}`);
```
## Key Insights
- Leyr applies idempotent behavior here: if the patient already exists in the EHR, create requests will return the existing patient instead of creating a duplicate.
- For patient path parameters, use supported `patient_id` formats like `emr_id:*` or `se_personal_number:*`.
- Use [Find or Get Patient](http://localhost/api/quickstart/find-patient) to determine the right identifier for downstream flows.
- `emr_id` is useful for EHR-side tracing and support.
- Some EHRs allow direct national identifier lookup in path parameters, for example `se_personal_number:{value}`.
## Next steps
Advanced topics and best practices.
Understand Leyr error codes and how to respond to them.
## API Reference
- [Patients API](https://api.leyr.io/docs#tag/Patients)
- [Patient ID Guide](http://localhost/api/guides/patient-id)
## Sitemap
See the full [sitemap](http://localhost/sitemap.md) for all pages.