# How to OAuth

Depending on your usage, you can implement this with pop-up window or redirection

## Popup window method

Add a message event listener like the following

```javascript
window.addEventListener('message', async function(event) {
    console.log(event)
    // Check for the user data in the message
    if (event.data?.type === 'well-oauth-token' && event.data?.token) {
        // verify and store the token in a secure way
        const jwtToken = event.data?.token;
        // Close the popup window
        if (popup) {
            popup.close();
        }
    }
});
```

## Redirect method

Set location.href

```javascript
location.href = `https://api.well.eco/oauth/auth?state=${randomString()}${Math.floor(Math.random() * 100000)}&redirect_url=${encodeURIComponent(yourURL)}`;
```

If the oauth success, the token will be included as the search param to the redirect\_url provided.

You can get the redirect token with the following code:

```javascript
const initialToken = new URLSearchParams(location.search).get('token');
```

After getting the token, you can request the user information with the following API

## Fetch with token

<mark style="color:green;">`GET`</mark> [`https://api.well.eco/oauth/me`](https://api.well.eco/oauth/me)

**Headers**

| Name         | Value                 |
| ------------ | --------------------- |
| Content-Type | `application/json`    |
| oauth-token  | {`jwt_token_you_get}` |

**Body(JSON**

<table><thead><tr><th width="190">Name</th><th width="106">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>id</code></td><td>string</td><td>id of the user</td></tr><tr><td><code>linkedAddress</code></td><td>string</td><td>linked ETH address</td></tr><tr><td><code>token</code></td><td>Object</td><td>Type as below</td></tr></tbody></table>

```javascript
{
  "description": string,
  "image": string,
  "tokenId": string,
  "name": string,
  "attributes": [
    {
      "trait_type": string,
      "value": string
    }
  ],
  "createdAt": string
}
```

**Response**

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

```json
{
  "id": "nX9d7H3cPQWmxrK2t3jh4UzIcGo5",
  "linkedAddress": "0x3F8B9a45678dCd872e93Be582f54AFe912b874Dc",
  "token": {
    "description": "Hello test.well, Well ID is the digital identity representing you in the ever growing network of wellness. Powered by WELL3.",
    "image": "https://s3.well3.com/WellID/7752/1.jpg",
    "tokenId": "77522218045247854526172210698749283758228288462773800836215567039694054862682",
    "name": "test.well",
    "attributes": [
      {
        "trait_type": "Type",
        "value": "Well ID"
      }
    ],
    "createdAt": "1713175672123"
  }
}
```

{% endtab %}

{% tab title="400" %}

```json
{
  "error": "Invalid request"
}
```

{% endtab %}
{% endtabs %}

## Telegram mini app oauth

1. Use the update account API to update your "tgResultCallbackURL", "tgBotID", "tgBotMiniAppURL"
2. Add the following code to your link/login well3 button callback

```javascript
window.open(`https://api.well.eco/oauth/tg-mini-app?oauth_app_id=${YOUR_WELL_APP_ID}`, '_blank');
```

3. Receive the result callback in your backend. The request will be a GET request, with a jwt attached to the API query. Nodejs example code:

```javascript
import jwt from 'jsonwebtoken';
router.get('/tg-well-oauth-callback', async (req, res, next) => {
  const decoded = jwt.verify(req.query.jwt, WELL_OAUTH_PROJECT_SECRET);
  const {tgUserID, token} = decoded;
  // Code to bind the user in your server with the access token
  await redis.set(`tg-test:${tgUserID}`, token);
  res.send();
});
```

4. Our telegram bot will have a button to redirect the user back to your telegram mini app with the tgBotMiniAppURL you provided, so it is important to fill in the URL.
5. When the user go back to your bot, fetch the access token stored in your server to get the Well user info.


---

# 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.well.eco/api-integration/publish-your-docs.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.
