# Creating Program Memberships for New Users

### How Program Memberships Work in Hang

A program membership in Hang represents a user's participation in a loyalty or rewards program. This can include the user's social media credentials, and other custom fields you might define for your program. With the Hang API, you can seamlessly add new members, making it easier to manage and expand your loyalty programs.

## Registering New Program Memberships

Creating a new program membership is as simple as making a **`POST`** request to the [`/v2/program-memberships`](broken://pages/8eqT8axl5iyR7wIoJ1Gr#v2-program-memberships) endpoint. NOTE: This can be used as a get or create, meaning if you try to create a user with the same phone number or external\_user\_id, the existing membership will be returned.

### Request Parameters

* `external_user_id` (optional): The user's external user ID. This must be provided if `phone` is not given.
* `email` (optional): The user's email address. If this is provided with a wallet address, and the brand is on shopify, we will tag the user in shopify.
* `phone`(optional - required if using the Hang portal): The user's phone number. If this is provided, this will be the users unique identifier for the program.
* `first_name` (optional): The user's first name.
* `last_name` (optional): The user's last name.
* `birthday` (optional): The user's birthday ("YYYY-MM-DD").
* `include_magic_link` (optional): Whether to return a magic link URL. Defaults to false.
* `family_member_birthdays` (optional): An array of objects, each representing a family member. Each object can include the following fields:
  * `first_name` (required): The first name of the family member.
  * `last_name` (optional): The last name of the family member.
  * `birthday` (required): The birthday ("YYYY-MM-DD") of the family member.
  * `status` (required): The status of the family member ("active"/"inactive").

{% hint style="info" %}
**Note:** At least one of  **`phone or external_user_id`** must be provided
{% endhint %}

#### Example Request

Here's an example of a valid POST request to the program memberships endpoint:

```bash
curl -X POST 'https://loyalty.hang.xyz/partner-api/v2/program-memberships' \
  -H 'X-API-Key: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "external_user_id": "user123",
    "phone": "+18884546767",
    "first_name": "Joe",
    "last_name": "Smith",
    "birthday": "1969-02-25",
    "include_magic_link": "true"
    "family_member_birthdays": [
      {
        "first_name": "Anna", 
        "birthday": "2000-02-02", 
        "status": "active" 
      },
      {
        "first_name": "Mason",
        "last_name": "Smith",
        "birthday": "2002-01-01", 
        "status": "active" 
      }
    ]
  }'
```

### Response

If successful, the API will return a 200 OK status along with the details of the newly created or existing program membership.

#### Example Response

```json
{
  "membership": {
    "id": "membership_id_1",
    "external_user_id": "user123",
    "created_at": 1633887337,
    "social_media_credentials": {
      "twitter_username": "user_twitter",
      "instagram_username": "user_instagram",
      "tiktok_username": "user_tiktok"
    },
    "magic_link": "authenticated_url",
    "family_member_birthdays": [
      {
        "first_name": "Anna",
        "last_name": "",
        "birth_date": "2000-02-02",
        "status": "active"
      },
      {
        "first_name": "Mason",
        "last_name": "Smith",
        "birth_date": "2002-01-01",
        "status": "active"
      }
    ]
  }
}
```

### Implementing Program Memberships in Your Backend

After making the API call, Hang will either create a new program membership or find an existing one based on the provided `external_user_id` and/or `phone`. Use the returned membership data to link your users with your loyalty programs.

### Error Handling

If the API encounters an error, a relevant error message will be returned to guide you through the troubleshooting process. For example, if both `external_user_id` and `phone` are missing, a `400 Bad Request` will be returned with the message 'Missing required parameters.'


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hang.com/hang-platform/api-usage/creating-program-memberships-for-new-users.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
