Export Patient Data to your CRM
Welcome to the Semble GraphQL API how-to guide. In this guide, we will show you how to use the Semble GraphQL public API to export data from Semble and maintain a live data feed of patient emails for your marketing. This process will ensure you're always working with the most recent data.
Step 1: Set Up Your API Call
The query you will be using is the patients query. This query allows you to fetch a list of patients, with customisation options to specify the data you are interested in.
Here's the basic structure of the query:
query patients(
$search: String,
$pagination: Pagination,
$options: QueryOptions
): PatientData
You need to replace $search, $pagination, and $options with the variables that fit your needs.
Step 2: Define Query Variables
In the QueryOptions, you'll want to use the updatedAt parameter to ensure you're fetching the patients that have changed since your last API call.
Here's an example of how you can set up the QueryOptions:
input QueryOptions {
updatedAt: {start: "2023-01-01T00:00:00.000z", end: "2023-01-01T23:59:59.000z"}
}
In this example, we are fetching the patients that were updated during a specific day. The start and end timestamps should be replaced with the appropriate time range for your needs.
Step 3: Make the API Call
Now that you have defined your query and variables, you can use your chosen method (such as cURL, JavaScript, Python, etc.) to send a POST request to the Semble API.
- cURL
- Javascript
- Python
curl 'https://open.semble.io/' \
-H 'Content-Type: application/json' \
-H 'x-token: YOUR_API_TOKEN' \
-d '{
"query": "query patients($pagination: Pagination, $options: QueryOptions) { patients(pagination: $pagination, options: $options) { data { id email updatedAt } } }",
"variables": {
"pagination": {"page": 1, "pageSize": 100},
"options": {"updatedAt": {"start": "2023-01-01T00:00:00.000z", "end": "2023-01-01T23:59:59.000z"}}
}
}'
For JavaScript, we'll use node-fetch, a lightweight module that brings the Fetch API to Node.js. Make sure to install it first using npm install node-fetch.
const fetch = require('node-fetch');
const API_URL = 'https://open.semble.io/';
const API_TOKEN = 'YOUR_API_TOKEN';
const query = `
query patients($pagination: Pagination, $options: QueryOptions) {
patients(pagination: $pagination, options: $options) {
data {
id
email
updatedAt
}
}
}`;
const variables = {
pagination: { page: 1, pageSize: 100 },
options: {
updatedAt: {
start: '2023-01-01T00:00:00.000z',
end: '2023-01-01T23:59:59.000z',
},
},
};
fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-token': API_TOKEN,
},
body: JSON.stringify({
query,
variables,
}),
})
.then((res) => res.json())
.then((res) => console.log(res.data))
.catch((err) => console.error(err));
For Python, we'll be using the requests library to make the HTTP request. Here's how you can set it up:
import requests
import json
API_URL = 'https://open.semble.io/'
API_TOKEN = 'YOUR_API_TOKEN'
headers = {
'Content-Type': 'application/json',
'x-token': API_TOKEN,
}
query = """
query patients($pagination: Pagination, $options: QueryOptions) {
patients(pagination: $pagination, options: $options) {
data {
id
email
updatedAt
}
}
}
"""
variables = {
"pagination": {"page": 1, "pageSize": 100},
"options": {"updatedAt": {"start": "2023-01-01T00:00:00.000z", "end": "2023-01-01T23:59:59.000z"}}
}
response = requests.post(API_URL, headers=headers, json={'query': query, 'variables': variables})
if response.status_code == 200:
print(response.json()['data'])
else:
print(f"Query failed with status code {response.status_code}")
Now, you're able to make the API call to Semble's GraphQL API using both JavaScript and Python to fetch updated patient data for your marketing needs.
Replace YOUR_API_TOKEN with your actual API token. This call will fetch the first 100 patients that were updated during the specified day.
Step 4: Handle the API Response
The response from the Semble API will include a list of patients, with their id, email, and updatedAt fields. You can then export this data, or feed it into your marketing system, ensuring that you always have up-to-date email addresses for your patients.
For example, this is how you would upload this data to HubSpot
First, we need to make HTTP requests to HubSpot's APIs. Below are examples of how to do this in cURL, JavaScript, and Python.
- You must first get your HubSpot API Key.
- Use this API Key to make a POST request to HubSpot's Contacts API.
The endpoint you'll be using is https://api.hubapi.com/contacts/v1/contact.
The body of your request will contain the patient's email address and any other data you would like to send.
- curl
- Javascript
- Python
curl -X POST \
-H 'Content-Type: application/json' \
-H 'hapikey: YOUR_HUBSPOT_API_KEY' \
'https://api.hubapi.com/contacts/v1/contact' \
-d '{
"properties": [
{
"property": "email",
"value": "example@example.com"
},
{
"property": "firstname",
"value": "John"
},
{
"property": "lastname",
"value": "Doe"
}
]
}'
const fetch = require('node-fetch');
const HUBSPOT_API_KEY = 'YOUR_HUBSPOT_API_KEY';
const url = `https://api.hubapi.com/contacts/v1/contact?hapikey=${HUBSPOT_API_KEY}`;
const data = {
properties: [
{
property: 'email',
value: 'example@example.com',
},
{
property: 'firstname',
value: 'John',
},
{
property: 'lastname',
value: 'Doe',
},
],
};
fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
},
})
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error('Error: ' + response.statusText);
}
})
.then((responseJson) => {
console.log(responseJson);
})
.catch((error) => {
console.log(error);
});
import requests
import json
HUBSPOT_API_KEY = 'YOUR_HUBSPOT_API_KEY'
url = f'https://api.hubapi.com/contacts/v1/contact?hapikey={HUBSPOT_API_KEY}'
data = {
"properties": [
{
"property": "email",
"value": "example@example.com"
},
{
"property": "firstname",
"value": "John"
},
{
"property": "lastname",
"value": "Doe"
}
]
}
response = requests.post(url, headers={"Content-Type": "application/json"}, data=json.dumps(data))
if response.status_code == 200:
print(response.json())
else:
print(f"Request failed with status code {response.status_code}")
Make sure to replace 'YOUR_HUBSPOT_API_KEY' with your actual HubSpot API key and adjust the property values in the data payload to match the patient information obtained from Semble's API.
Remember to store the timestamp of your most recent API call, so that for the next call, you can set the updatedAt field to fetch only the patients updated since your last call.
By following these steps, you can create a live data feed of patient emails for your marketing purposes, ensuring you're always working with the most up-to-date information.