# Petje.af documentatie

Documentatie voor content creators met een pagina op petje.af

{% content-ref url="/pages/-Lw3wbIpGuQLfi7dWe3p" %}
[Petje.af API](https://docs.petje.af/petje.af-api/introduction)
{% endcontent-ref %}


# Introduction

The Petje.af API allows developers to use the OAuth2 protocol to allow a Petje.af user to grant a 3rd party application partial access to his/her account.

## Why the Petje.af API?

The Petje.af API is meant for creating a `Petje.af login` on your own website or application. It's a selection of GET endpoints to receive information about the authenticated user's memberships and pages.&#x20;


# Authentication

The Petje.af API allows developers to use the OAuth2 protocol to allow a Petje.af user to grant a 3rd party application partial access to his/her account.

## Introduction

### 1. Registering your client application

Before integrating the Petje.af API, you’ll need to register a new OAuth2 application in your Petje.af dashboard.

![Go to "Koppelingen > Apps" for creating a client application](https://3983081690-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lh9M5OcufDN8F3i6rFD%2Fuploads%2FJmYU83EXNnMzWoZRIcpf%2FScreenshot%202026-06-16%20at%2014.26.23.png?alt=media\&token=f71af6bd-5028-4e08-87b3-24ca8151ca76)

### **2. Redirect users to request access**

Redirect users form your application to [the authorize page](https://docs.petje.af/petje.af-api/authentication/authorize) using the necessary scopes. On this page the user can grant authorization to your client application for [the scopes](https://docs.petje.af/petje.af-api/authentication/scopes) you requested.

### 3. Petje.af redirects back to your site

If the user approves your application, Petje.af will redirect them back to your `redirect_uri` with a temporary `code` parameter.

Example of the redirect:

```
GET https://example.com/oauth/callback?code=CODE&state=STATE
```

### 4. **Exchange code for an access token**

After you have received the temporary code, you can exchange it for valid access and refresh tokens using t[he tokens endpoint](https://docs.petje.af/petje.af-api/authentication/tokens).

### 5. Refreshing tokens

Because the Petje.af API issues short-lived access tokens, you will need to refresh access tokens using [the tokens endpoint](https://docs.petje.af/petje.af-api/authentication/tokens) via the refresh token that was provided when the access token was issued.


# Scopes

OAuth2 authentication require that you obtain correct permissions (scopes) to access different API endpoints.

Scopes should be considered as grants: Users can select which scopes they grant access to for the application. The application might need to request new scopes over the lifecycle of the authorization.

As a general rule, you should only ask for scopes which your application needs and avoid asking for access to unnecessary ones. Users more readily grant access to limited, clearly described scopes.

| Scope            | Description                              |
| ---------------- | ---------------------------------------- |
| profile.read     | View the user profile                    |
| memberships.read | View the user's memberships and rewards  |
| pages.read       | View the user's pages, plans and rewards |
| webhooks.read    | View the use's page webhooks             |
| webhooks.write   | Create, update and delete page webhooks  |


# Authorize

The Authorize endpoint is a page on the Petje.af website where the logged in user grants authorization to your client application.

## Authorize

<mark style="color:blue;">`GET`</mark> `https://petjeaf.com/oauth2/authorize`

On this page on the Petje.af platform the logged in user can grant the authorization to your client application for the scopes you have requested. \
\
Petje.af will then redirect the user back to the `redirect_uri` you have specified. The `redirect_uri` will be appended with a `code` parameter, which will contain the `authorization code`. You should then exchange the `authorization code` for an `access token` and a `refresh token` using the tokens endpoint.

#### Path Parameters

| Name           | Type   | Description                                                                                                                               |
| -------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------- |
| redirect\_uri  | string | The URL the user is send back to ones the request has been authorized. If given, it must macht the URL you set when registering your app. |
| scope          | string | A space separated list of permissions your app requires.                                                                                  |
| state          | string | A random string generated by your app to prevent CSRF attacks                                                                             |
| response\_type | string | <p>Petje.af only replies with code responses<br><br>Possible values: <code>code</code></p>                                                |
| client\_id     | string | The client ID of your client application                                                                                                  |

{% tabs %}
{% tab title="301 " %}

```
After granting authorization the user will be redirected to your redirect_uri
with the following query parameters:

code: The authorization code with which you can request an access token
state: The random string you've sent with your request to prevent CSRF attacks
```

{% endtab %}
{% endtabs %}


# Tokens

Get a valid access and refresh token.

## Generate tokens

<mark style="color:green;">`POST`</mark> `https://api.petjeaf.com/v1/oauth2/tokens`

This endpoint allows you to exchange an `authorization_code` or `refresh_token` for a valid `access_token`\
\
**Use `application/x-www-form-urlencoded` for the POST request**

#### Request Body

| Name           | Type   | Description                                                                                                                                                                                     |
| -------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| client\_id     | string | The client ID from your client app                                                                                                                                                              |
| client\_secret | string | The client secret key from your Client App                                                                                                                                                      |
| grant\_type    | string | If you wish to exchange your auth code for an access token, use grant type `authorization_code`. If you wish to renew your access token with your refresh token, use grant type `refresh token` |
| code           | string | The authorization code you've received when creating the authorization. This field is required when using gran type `authorization_code`                                                        |
| refresh\_token | string | The refresh token you've received when creating the authorization. This field is required when using grant type `refresh_token`                                                                 |

{% tabs %}
{% tab title="200 Tokens successfully retrieved. The access\_token is a JSON Web Token with the name, email and id after decoding." %}

```
{
    "access_token": "ACCESS TOKEN (JWT)",
    "token_type": "Bearer",
    "expires_in": 3599,
    "refresh_token": "REFRESH TOKEN",
    "scope": "memberships.read"
}
```

{% endtab %}

{% tab title="400 There was an error while creating your tokens" %}

```
{
    "status": 400,
    "title": "Bad Request",
    "detail": "Invalid grant: authorization code is invalid",
    "_links": {
        "documentation": {
            "href": "https://docs.petje.af",
            "type": "text/html"
        }
    }
}
```

{% endtab %}
{% endtabs %}


# Reference

The endpoints of the Petje.af API.

{% content-ref url="/pages/-Lw42MMk3JOpxH3OpkUa" %}
[Pages](https://docs.petje.af/petje.af-api/reference/pages)
{% endcontent-ref %}

{% content-ref url="/pages/-Lw44pLj4piJLbV7J79n" %}
[Plans](https://docs.petje.af/petje.af-api/reference/plans)
{% endcontent-ref %}

{% content-ref url="/pages/-Lw47Ph9huZP9\_U5KWmF" %}
[Rewards](https://docs.petje.af/petje.af-api/reference/rewards)
{% endcontent-ref %}

{% content-ref url="/pages/-Lw3wmITpDyOMz9uPblv" %}
[Memberships](https://docs.petje.af/petje.af-api/reference/memberships-1)
{% endcontent-ref %}

{% content-ref url="/pages/-Lw40MrJJOB7d5ZHymvc" %}
[Membership rewards](https://docs.petje.af/petje.af-api/reference/membership-rewards)
{% endcontent-ref %}


# Profile

Get the Profile of the current user

## Get current profile

<mark style="color:blue;">`GET`</mark> `https://api.petjeaf.com/v1/users/me`

This endpoint allows you to get the profile of the authenticated user.\
\
Scope: `profile.read`

#### Headers

| Name          | Type   | Description                            |
| ------------- | ------ | -------------------------------------- |
| Authorization | string | Access token using the `Bearer` method |

{% tabs %}
{% tab title="200 " %}

```
{
    "resource": "users",
    "id": "USER ID",
    "name": "John Doe",
    "email": "johndoe@petje.af",
    "_links": {
        "self": {
            "href": "https://api.petje.af/users/me",
            "type": "application/hal+json"
        }
    }
}
```

{% endtab %}
{% endtabs %}


# Pages

The pages that belong to the authenticated user.

## Get pages

<mark style="color:blue;">`GET`</mark> `https://api.petjeaf.com/v1/pages`

This endpoint allows you to get pages that belongs to the authenticated user.\
\
Scope: `pages.read`

#### Query Parameters

| Name  | Type    | Description                                            |
| ----- | ------- | ------------------------------------------------------ |
| limit | integer | The number of pages to return                          |
| from  | string  | Offset the results to start after the provided page ID |

#### Headers

| Name          | Type   | Description                            |
| ------------- | ------ | -------------------------------------- |
| Authorization | string | Access token using the `Bearer` method |

{% tabs %}
{% tab title="200 Pages successfully retrieved." %}

```
{
    "count": 4,
    "_embedded": {
        "pages": [
            {
                "resource": "pages",
                "id": "5SaEUOWD2b4dN7tJBKMB",
                "userId": "8jFy8VbRPTfkM1658U2IrwvIV7R2",
                "name": "Test 8",
                "slug": "test8",
                "validated": true,
                "_links": {
                    "self": {
                        "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB",
                        "type": "application/hal+json"
                    },
                    "plans": {
                        "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB/plans",
                        "type": "application/hal+json"
                    },
                    "rewards": {
                        "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB/rewards",
                        "type": "application/hal+json"
                    }
                }
            },
            ...
        ]
    },
    "_links": {
        "self": {
            "href": "https://api.petje.af/pages",
            "type": "application/hal+json"
        },
        "next": null
    }
}
```

{% endtab %}
{% endtabs %}

## Get page detail

<mark style="color:blue;">`GET`</mark> `https://api.petje.af/v1/pages/:id`

Scope: `pages.read`

#### Path Parameters

| Name   | Type   | Description |
| ------ | ------ | ----------- |
| pageId | string | The page ID |

#### Headers

| Name          | Type   | Description                            |
| ------------- | ------ | -------------------------------------- |
| Authorization | string | Access token using the `Bearer` method |

{% tabs %}
{% tab title="200 " %}

```
{
    "resource": "pages",
    "id": "XdvyMNuQArs6KGRANDlT",
    "userId": "8jFy8VbRPTfkM1658U2IrwvIV7R2",
    "name": "Test 6",
    "slug": "test6",
    "validated": true,
    "_links": {
        "self": {
            "href": "https://api.petje.af/pages/XdvyMNuQArs6KGRANDlT",
            "type": "application/hal+json"
        },
        "plans": {
            "href": "https://api.petje.af/pages/XdvyMNuQArs6KGRANDlT/plans",
            "type": "application/hal+json"
        },
        "rewards": {
            "href": "https://api.petje.af/pages/XdvyMNuQArs6KGRANDlT/rewards",
            "type": "application/hal+json"
        }
    }
}
```

{% endtab %}

{% tab title="404 " %}

```
{
    "status": 404,
    "title": "Not Found",
    "detail": "Page is not found",
    "_links": {
        "documentation": {
            "href": "https://docs.petje.af",
            "type": "text/html"
        }
    }
}
```

{% endtab %}
{% endtabs %}


# Plans

The plans that belong to a page that belongs to the authenticated user.

## Get plans

<mark style="color:blue;">`GET`</mark> `https://api.petjeaf.com/v1/pages/:pageId/plans`

This endpoint allows you to get the plans that belongs to the selected page.\
\
Scope: `pages.read`

#### Path Parameters

| Name   | Type   | Description                             |
| ------ | ------ | --------------------------------------- |
| pageId | string | ID of the page that the plans belong to |

#### Query Parameters

| Name     | Type    | Description                                    |
| -------- | ------- | ---------------------------------------------- |
| rewardId | string  | Filter the results by this Reward ID           |
| limit    | string  | The number of plans to return                  |
| from     | boolean | Offset the results after the plan with this ID |

#### Headers

| Name          | Type   | Description                            |
| ------------- | ------ | -------------------------------------- |
| Authorization | string | Access token using the `Bearer` method |

{% tabs %}
{% tab title="200 Cake successfully retrieved." %}

```
{
    "count": 3,
    "_embedded": {
        "plans": [
            {
                "resource": "plans",
                "id": "AUmpLHxzVEU3R57RtFxM",
                "name": "Klein petje",
                "amount": 3,
                "interval": "month",
                "intervalLabel": "publicatie",
                "rewards": [
                    {
                        "id": "6JGhzAiPD7jqlbYkdrg8",
                        "name": "Dit is een beloning",
                        "type": "default"
                    },
                    {
                        "id": "8igf2PLGpYjLLJp3NaTB",
                        "name": "Instagram account",
                        "type": "instagram"
                    }
                ],
                "status": "enabled",
                "_links": {
                    "self": {
                        "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB/plans/AUmpLHxzVEU3R57RtFxM",
                        "type": "application/hal+json"
                    },
                    "page": {
                        "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB",
                        "type": "application/hal+json"
                    },
                    "rewards": {
                        "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB/rewards?planId=AUmpLHxzVEU3R57RtFxM",
                        "type": "application/hal+json"
                    }
                }
            },
            ...
        ]
    },
    "_links": {
        "self": {
            "href": "https://api.petje.af/plans",
            "type": "application/hal+json"
        },
        "next": null
    }
}
```

{% endtab %}
{% endtabs %}

## Get plan detail

<mark style="color:blue;">`GET`</mark> `https://api.petje.af/v1/pages/:pageId/plans/:id`

Scope: `pages.read`

#### Path Parameters

| Name   | Type   | Description |
| ------ | ------ | ----------- |
| pageId | string | The page ID |
| id     | string | The plan ID |

#### Headers

| Name          | Type   | Description                            |
| ------------- | ------ | -------------------------------------- |
| Authorization | string | Access token using the `Bearer` method |

{% tabs %}
{% tab title="200 " %}

```
{
    "resource": "plans",
    "id": "AUmpLHxzVEU3R57RtFxM",
    "name": "Klein petje",
    "amount": 3,
    "interval": "month",
    "intervalLabel": "publicatie",
    "status": "enabled",
    "_links": {
        "self": {
            "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB/plans/AUmpLHxzVEU3R57RtFxM",
            "type": "application/hal+json"
        },
        "page": {
            "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB",
            "type": "application/hal+json"
        },
        "rewards": {
            "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB/rewards?planId=AUmpLHxzVEU3R57RtFxM",
            "type": "application/hal+json"
        }
    }
}
```

{% endtab %}

{% tab title="404 " %}

```
{
    "status": 404,
    "title": "Not Found",
    "detail": "Plan is not found",
    "_links": {
        "documentation": {
            "href": "https://docs.petje.af",
            "type": "text/html"
        }
    }
}
```

{% endtab %}
{% endtabs %}


# Rewards

The rewards that belong to a page that belongs to the authenticated user

## Get page rewards

<mark style="color:blue;">`GET`</mark> `https://api.petjeaf.com/v1/page/:pageId/rewards`

This endpoint allows you to get the rewards that belongs to the selected page.\
\
Scope: `pages.read`

#### Path Parameters

| Name   | Type   | Description |
| ------ | ------ | ----------- |
| pageId | string | The page ID |

#### Query Parameters

| Name   | Type    | Description                                         |
| ------ | ------- | --------------------------------------------------- |
| planId | string  | Filter the results by this Plan ID                  |
| limit  | integer | The number of rewards to return                     |
| from   | boolean | Offset the results to after the reward with this ID |

#### Headers

| Name          | Type   | Description                            |
| ------------- | ------ | -------------------------------------- |
| Authorization | string | Access token using the `Bearer` method |

{% tabs %}
{% tab title="200 Cake successfully retrieved." %}

```
{
    "count": 3,
    "_embedded": {
        "rewards": [
            {
                "resource": "rewards",
                "id": "6JGhzAiPD7jqlbYkdrg8",
                "name": "Dit is een beloning",
                "description": "Hallo daar!",
                "type": "default",
                "status": "enabled",
                "_links": {
                    "self": {
                        "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB/rewards/6JGhzAiPD7jqlbYkdrg8",
                        "type": "application/hal+json"
                    },
                    "page": {
                        "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB",
                        "type": "application/hal+json"
                    },
                    "plans": {
                        "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB/plans?rewardId=6JGhzAiPD7jqlbYkdrg8",
                        "type": "application/hal+json"
                    }
                }
            },
            ...
        ]
    },
    "_links": {
        "self": {
            "href": "https://api.petje.af/rewards",
            "type": "application/hal+json"
        },
        "next": null
    }
}
```

{% endtab %}
{% endtabs %}

## Get page reward detail

<mark style="color:blue;">`GET`</mark> `https://api.petje.af/v1/pages/:pageId/rewards/:id`

Scope: `pages.read`

#### Path Parameters

| Name   | Type   | Description   |
| ------ | ------ | ------------- |
| pageId | string | The page ID   |
| id     | string | The reward ID |

#### Headers

| Name          | Type   | Description                            |
| ------------- | ------ | -------------------------------------- |
| Authorization | string | Access token using the `Bearer` method |

{% tabs %}
{% tab title="200 " %}

```
{
    "resource": "rewards",
    "id": "6JGhzAiPD7jqlbYkdrg8",
    "name": "Dit is een beloning",
    "description": "Hall daar!",
    "type": "default",
    "status": "enabled",
    "_links": {
        "self": {
            "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB/rewards/6JGhzAiPD7jqlbYkdrg8",
            "type": "application/hal+json"
        },
        "page": {
            "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB",
            "type": "application/hal+json"
        },
        "plans": {
            "href": "https://api.petje.af/pages/5SaEUOWD2b4dN7tJBKMB/plans?rewardId=6JGhzAiPD7jqlbYkdrg8",
            "type": "application/hal+json"
        }
    }
}
```

{% endtab %}

{% tab title="404 " %}

```
{
    "status": 404,
    "title": "Not Found",
    "detail": "Page Reward is not found",
    "_links": {
        "documentation": {
            "href": "https://docs.petje.af",
            "type": "text/html"
        }
    }
}
```

{% endtab %}
{% endtabs %}


# Memberships

The memberships of the authenticated user.

## Get Memberships

<mark style="color:blue;">`GET`</mark> `https://api.petjeaf.com/v1/memberships`

This endpoint allows you to get all memberships that belongs to the authenticated user.\
\
Scope: `memberships.read`

#### Query Parameters

| Name   | Type    | Description                                                 |
| ------ | ------- | ----------------------------------------------------------- |
| pageId | string  | Filter the results for a specific page by the ID            |
| limit  | integer | The number of memberships to return (with a maximum of 250) |
| from   | string  | Offset the results after the membership with this ID.       |

#### Headers

| Name          | Type   | Description                          |
| ------------- | ------ | ------------------------------------ |
| Authorization | string | Access token using the Bearer method |

{% tabs %}
{% tab title="200 Memberships successfully retrieved." %}

```
{
    "count": 7,
    "_embedded": {
        "memberships": [
            {
                "resource": "memberships",
                "id": "HVogpphHSabylpg2gBYL",
                "userId": "8jFy8VbRPTfkM1658U2IrwvIV7R2",
                "amount": 3,
                "interval": "month",
                "intervalLabel": "publicatie",
                "number": 200001299,
                "pageId": "eKEI7z2RrLoZ1Gsr6Mxt",
                "pageName": "Test 2",
                "pageSlug": "test2",
                "plan": "Klein petje",
                "planId": "nEkZypHsCJqVkPaje9I6",
                "status": "active_end_month",
                "_links": {
                    "self": {
                        "href": "https://api.petje.af/memberships/HVogpphHSabylpg2gBYL",
                        "type": "application/hal+json"
                    },
                    "rewards": {
                        "href": "https://api.petje.af/memberships/HVogpphHSabylpg2gBYL/rewards",
                        "type": "application/hal+json"
                    }
                }
            },
            ...
        ]
    },
    "_links": {
        "self": {
            "href": "https://api.petje.af/memberships",
            "type": "application/hal+json"
        },
        "next": null
    }
}
```

{% endtab %}
{% endtabs %}

## Get membership detail

<mark style="color:blue;">`GET`</mark> `https://api.petje.af/v1/memberships/:id`

This endpoint allows you to get a specific membership that belongs to the authenticated user.\
\
Scope: `memberships.read`

#### Path Parameters

| Name | Type   | Description       |
| ---- | ------ | ----------------- |
| id   | string | The membership ID |

{% tabs %}
{% tab title="200 " %}

```
{
    "resource": "memberships",
    "id": "HVogpphHSabylpg2gBYL",
    "userId": "8jFy8VbRPTfkM1658U2IrwvIV7R2",
    "amount": 3,
    "interval": "month",
    "intervalLabel": "publicatie",
    "number": 200001299,
    "pageId": "eKEI7z2RrLoZ1Gsr6Mxt",
    "pageName": "Test 2",
    "pageSlug": "test2",
    "plan": "Klein petje",
    "planId": "nEkZypHsCJqVkPaje9I6",
    "status": "active_end_month",
    "_links": {
        "self": {
            "href": "https://api.petje.af/memberships/HVogpphHSabylpg2gBYL",
            "type": "application/hal+json"
        },
        "rewards": {
            "href": "https://api.petje.af/memberships/HVogpphHSabylpg2gBYL/rewards",
            "type": "application/hal+json"
        }
    }
}
```

{% endtab %}

{% tab title="404 " %}

```
{
    "status": 404,
    "title": "Not Found",
    "detail": "Membership is not found",
    "_links": {
        "documentation": {
            "href": "https://docs.petje.af",
            "type": "text/html"
        }
    }
}
```

{% endtab %}
{% endtabs %}


# Membership rewards

The rewards that belongs to a membership.

## Get membership rewards

<mark style="color:blue;">`GET`</mark> `https://api.petjeaf.com/v1/memberships/:membershipId/rewards`

Scope: `memberships.read`

#### Path Parameters

| Name         | Type   | Description          |
| ------------ | ------ | -------------------- |
| membershipId | string | ID of the membership |

#### Query Parameters

| Name  | Type    | Description                                     |
| ----- | ------- | ----------------------------------------------- |
| limit | integer | The number of rewards to return                 |
| from  | string  | Offset the result after the reward with this ID |

#### Headers

| Name          | Type   | Description                            |
| ------------- | ------ | -------------------------------------- |
| Authorization | string | Access token using the `Bearer` method |

{% tabs %}
{% tab title="200 Cake successfully retrieved." %}

```
{
    "count": 3,
    "_embedded": {
        "rewards": [
            {
                "resource": "rewards",
                "id": "i3DMlxfiU7bzqLtYmFUS",
                "name": "Sticker",
                "description": "Schitterende sticker!",
                "active": true,
                "user": {},
                "type": "parcel",
                "_links": {
                    "self": {
                        "href": "https://api.petje.af/memberships/HVogpphHSabylpg2gBYL/rewards/i3DMlxfiU7bzqLtYmFUS",
                        "type": "application/hal+json"
                    },
                    "membership": {
                        "href": "https://api.petje.af/memberships/HVogpphHSabylpg2gBYL",
                        "type": "application/hal+json"
                    }
                }
            },
            ...
        ]
    },
    "_links": {
        "self": {
            "href": "https://api.petje.af/rewards",
            "type": "application/hal+json"
        },
        "next": null
    }
}
```

{% endtab %}
{% endtabs %}

## Get membership reward detail

<mark style="color:blue;">`GET`</mark> `https://api.petje.af/v1/memberships/:membershipId/rewards/:id`

Scope: `memberships.read`

#### Path Parameters

| Name         | Type   | Description                 |
| ------------ | ------ | --------------------------- |
| id           | string | ID of the membership reward |
| membershipId | string | ID of the membership        |

#### Headers

| Name          | Type   | Description                          |
| ------------- | ------ | ------------------------------------ |
| Authorization | string | Access token using the Bearer method |

{% tabs %}
{% tab title="200 " %}

```
{
    "resource": "rewards",
    "id": "i3DMlxfiU7bzqLtYmFUS",
    "name": "Sticker",
    "description": "Schitterende sticker!",
    "active": true,
    "user": {},
    "type": "parcel",
    "_links": {
        "self": {
            "href": "https://api.petje.af/memberships/HVogpphHSabylpg2gBYL/rewards/i3DMlxfiU7bzqLtYmFUS",
            "type": "application/hal+json"
        },
        "membership": {
            "href": "https://api.petje.af/memberships/HVogpphHSabylpg2gBYL",
            "type": "application/hal+json"
        }
    }
}
```

{% endtab %}

{% tab title="404 " %}

```
{
    "status": 404,
    "title": "Not Found",
    "detail": "Membership Reward is not found",
    "_links": {
        "documentation": {
            "href": "https://docs.petje.af",
            "type": "text/html"
        }
    }
}

```

{% endtab %}
{% endtabs %}


# Webhooks

Webhooks allow your application to receive notifications when events occur on a page belonging to the authenticated user. Webhook endpoints require the authenticated user to own the selected page.

### Event types

A webhook can subscribe to one or more of the following events:

| Event                 | Description                                |
| --------------------- | ------------------------------------------ |
| `new_onetime_payment` | A new one-time payment has been paid       |
| `new_membership`      | A membership has been activated            |
| `membership_canceled` | A membership has been canceled             |
| `webhook_deleted`     | The webhook configuration has been deleted |

***

### Get webhooks

<mark style="color:blue;">`GET`</mark> `https://api.petje.af/v1/pages/:pageId/webhooks`

Returns the webhooks belonging to the selected page.

Scope: `webhooks.read`

#### Path Parameters

| Name     | Type   | Description    |
| -------- | ------ | -------------- |
| `pageId` | string | ID of the page |

#### Query Parameters

| Name    | Type    | Description                                             |
| ------- | ------- | ------------------------------------------------------- |
| `limit` | integer | Number of webhooks to return. Must be between 1 and 250 |
| `after` | string  | Return results after the webhook with this ID           |

#### Headers

| Name            | Type   | Description                            |
| --------------- | ------ | -------------------------------------- |
| `Authorization` | string | Access token using the `Bearer` method |

#### Responses

**200 — Webhooks successfully retrieved**

```json
{
    "count": 2,
    "_embedded": {
        "webhooks": [
            {
                "resource": "webhooks",
                "id": "8bcd47fa-6623-4bd9-98d5-f0b460f2f0f8",
                "url": "https://example.com/webhooks/petjeaf",
                "secret": "generated-webhook-secret",
                "events": [
                    "new_onetime_payment",
                    "new_membership"
                ],
                "_links": {
                    "self": {
                        "href": "https://api.petje.af/v1/pages/5SaEUOWD2b4dN7tJBKMB/webhooks/8bcd47fa-6623-4bd9-98d5-f0b460f2f0f8",
                        "type": "application/hal+json"
                    },
                    "page": {
                        "href": "https://api.petje.af/v1/pages/5SaEUOWD2b4dN7tJBKMB",
                        "type": "application/hal+json"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "https://api.petje.af/v1/pages/5SaEUOWD2b4dN7tJBKMB/webhooks",
            "type": "application/hal+json"
        },
        "next": null
    }
}
```

**403 — Missing scope or page access**

***

### Get webhook detail

<mark style="color:blue;">`GET`</mark> `https://api.petje.af/v1/pages/:pageId/webhooks/:id`

Returns a single webhook belonging to the selected page.

Scope: `webhooks.read`

#### Path Parameters

| Name     | Type   | Description       |
| -------- | ------ | ----------------- |
| `pageId` | string | ID of the page    |
| `id`     | string | ID of the webhook |

#### Headers

| Name            | Type   | Description                            |
| --------------- | ------ | -------------------------------------- |
| `Authorization` | string | Access token using the `Bearer` method |

#### Responses

**200 — Webhook successfully retrieved**

```json
{
    "resource": "webhooks",
    "id": "8bcd47fa-6623-4bd9-98d5-f0b460f2f0f8",
    "url": "https://example.com/webhooks/petjeaf",
    "secret": "generated-webhook-secret",
    "events": [
        "new_onetime_payment",
        "new_membership"
    ],
    "_links": {
        "self": {
            "href": "https://api.petje.af/v1/pages/5SaEUOWD2b4dN7tJBKMB/webhooks/8bcd47fa-6623-4bd9-98d5-f0b460f2f0f8",
            "type": "application/hal+json"
        },
        "page": {
            "href": "https://api.petje.af/v1/pages/5SaEUOWD2b4dN7tJBKMB",
            "type": "application/hal+json"
        }
    }
}
```

**403 — Missing scope or page access**

**404 — Webhook not found or does not belong to the page**

***

### Create webhook

`POST` `https://api.petje.af/v1/pages/:pageId/webhooks`

Creates a webhook for the selected page. The webhook secret is generated automatically and cannot be supplied by the client.

Scope: `webhooks.create` or `webhooks.write`

#### Path Parameters

| Name     | Type   | Description    |
| -------- | ------ | -------------- |
| `pageId` | string | ID of the page |

#### Headers

| Name            | Type   | Description                            |
| --------------- | ------ | -------------------------------------- |
| `Authorization` | string | Access token using the `Bearer` method |
| `Content-Type`  | string | `application/json`                     |

#### Request Body

| Name     | Type   | Required | Description                               |
| -------- | ------ | -------- | ----------------------------------------- |
| `url`    | string | Yes      | URL to which webhook requests are sent    |
| `events` | array  | Yes      | One or more supported webhook event types |

```json
{
    "url": "https://example.com/webhooks/petjeaf",
    "events": [
        "new_onetime_payment",
        "new_membership",
        "membership_canceled",
        "webhook_deleted"
    ]
}
```

#### Responses

**201 — Webhook successfully created**

```json
{
    "resource": "webhooks",
    "id": "8bcd47fa-6623-4bd9-98d5-f0b460f2f0f8",
    "url": "https://example.com/webhooks/petjeaf",
    "secret": "generated-webhook-secret",
    "events": [
        "new_onetime_payment",
        "new_membership",
        "membership_canceled",
        "webhook_deleted"
    ],
    "_links": {
        "self": {
            "href": "https://api.petje.af/v1/pages/5SaEUOWD2b4dN7tJBKMB/webhooks/8bcd47fa-6623-4bd9-98d5-f0b460f2f0f8",
            "type": "application/hal+json"
        },
        "page": {
            "href": "https://api.petje.af/v1/pages/5SaEUOWD2b4dN7tJBKMB",
            "type": "application/hal+json"
        }
    }
}
```

**403 — Missing scope or page access**

**422 — Validation failed**

The following conditions cause a validation error:

* `url` is missing or is not a valid URL.
* `events` is missing or empty.
* `events` contains an unsupported or duplicate event type.

***

### Delete webhook

`DELETE` `https://api.petje.af/v1/pages/:pageId/webhooks/:id`

Deletes a webhook belonging to the selected page.

If the webhook subscribes to `webhook_deleted`, a final asynchronous request is sent to the configured URL after deletion.

Scope: `webhooks.delete` or `webhooks.write`

#### Path Parameters

| Name     | Type   | Description       |
| -------- | ------ | ----------------- |
| `pageId` | string | ID of the page    |
| `id`     | string | ID of the webhook |

#### Headers

| Name            | Type   | Description                            |
| --------------- | ------ | -------------------------------------- |
| `Authorization` | string | Access token using the `Bearer` method |

#### Responses

**204 — Webhook successfully deleted**

The response does not contain a body.

**403 — Missing scope or page access**

**404 — Webhook not found or does not belong to the page**

***

## Receiving webhook requests

Webhook requests are sent as HTTP `POST` requests containing JSON.

### Request headers

| Header                        | Description                                   |
| ----------------------------- | --------------------------------------------- |
| `Content-Type`                | `application/json`                            |
| `X-Petjeaf-Webhook-Event`     | The event type                                |
| `X-Petjeaf-Webhook-Signature` | HMAC-SHA256 signature prefixed with `sha256=` |

The signature is created from the raw JSON request body using the webhook secret.

```
sha256=HMAC_SHA256(raw_request_body, webhook_secret)
```

Your endpoint should calculate the signature using the raw request body and compare it using a timing-safe comparison.

Failed webhook deliveries are retried up to three times.

### One-time payment payload

Event: `new_onetime_payment`

```json
{
    "event": "new_onetime_payment",
    "data": {
        "amount": 5,
        "currency": "EUR",
        "formatted_amount": "€ 5,00",
        "id": "f4476dbc-19fd-4134-a10f-a5893127db97",
        "page": {
            "id": "5SaEUOWD2b4dN7tJBKMB",
            "name": "Petje.af creator",
            "title": "Support my work"
        },
        "user": {
            "name": "John Doe",
            "id": "ea0fcc69-e615-48d2-a0d7-7c08d49988a1"
        },
        "comment": {
            "id": "ad040078-65d7-45aa-b576-78764970e279",
            "comment": "Keep up the good work!",
            "visibility": "public"
        }
    }
}
```

The `comment`, `page`, or `user` value can be `null` when the associated resource is unavailable.

### Membership payload

Events:

* `new_membership`
* `membership_canceled`

```json
{
    "event": "new_membership",
    "data": {
        "amount": 2,
        "currency": "EUR",
        "interval": "month",
        "amount_formatted": "€ 2,00 per maand",
        "id": "9126eabb-3444-4635-aeba-e10876102e70",
        "page": {
            "id": "5SaEUOWD2b4dN7tJBKMB",
            "name": "Petje.af creator",
            "title": "Support my work"
        },
        "user": {
            "name": "Jane Doe",
            "id": "4098f3fe-50ec-4482-8959-8f623764e1dc"
        },
        "comment": {
            "id": "99d44683-bfa4-40da-8214-c44f0b686fa2",
            "comment": "Happy to join!",
            "visibility": "public"
        }
    }
}
```

The `comment`, `page`, or `user` value can be `null` when the associated resource is unavailable.

### Webhook deleted payload

Event: `webhook_deleted`

```json
{
    "event": "webhook_deleted",
    "data": {
        "id": "8bcd47fa-6623-4bd9-98d5-f0b460f2f0f8",
        "page": {
            "id": "5SaEUOWD2b4dN7tJBKMB",
            "name": "Petje.af creator",
            "title": "Support my work"
        }
    }
}
```


