Parsing an API Response
This guide will help you parse an API response.
In this example, you will store part of the response in a Property for later reference using GraphQL mutations in callback
, since you are able to access response body via response.body
.
The tutorial assumes the API returns JSON like:
{
"third_party_api_id": "id-1"
}
and you want store the third_party_api_id
value (which is id-1
).
Requirements
To follow the steps in this tutorial, you should understand the concept of users and Properties. You'll use the user profile and API Call Notification created in previous tutorials.
- Users
- Properties
- Creating a User Profile (previous tutorial)
- Creating an API Call Notification (previous tutorial)
Steps
Parsing an API response is a four-step process:
Step 1: Create Property
As a first step, create a Property in the user profile. In this example, you will use default_profile
.
Step 2: Add GraphQL mutation
Add GraphQL mutation:
app/graphql/store_third_party_api_id.graphql
mutation store_third_party_api_id($user_id: ID!, $third_party_api_id: String!) {
user_update(
id: $user_id
user: {
profiles: [
{
name: "default",
values: {
properties: [{ name: "third_party_api_id", value: $third_party_api_id }]
}
}
]
}
form_name: "callback_third_party_api_id"
) {
id
}
}
Step 3: Create Form
The mutation you created provides input for the Form called callback_third_party_api_id
, so create this Form:
app/forms/callback_third_party_api_id.liquid
---
resource: User
fields:
profiles:
default:
properties:
third_party_api_id:
---
Step 4: Extend API Call Notification with callback
Extend the API Call Notification by adding the proper callback, which executes the mutation with the input received form the third party API:
app/api_calls/ping_example_com_on_user_sign_up.liquid
---
to: 'https://example.com/endpoint/{{ form.id }}'
callback: "{%- assign response_json = response.body | to_hash -%}{% graphql res = 'persist_custom_attribute', id: form.id, third_party_api_id: response_json.third_party_api_id %}"
---