> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mindset.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Back end authentication

> Embedding an agent, Back-end script.

## Back-end Script

On your server, you should create an end point (called by your front-end) which will create a session for a user.

To create that user session you will need to call the Mindset AI **SDK Users API** (see the [SDK Users API documentation](/sdk-api/api/sdkusers-api) for more details).

You must provide an `externalId` or a `userEmail` parameter so that the Mindset system can identify the user.

<Warning>
  You can learn more about the user email address : [User Handling](https://docs.mindset.ai/deploy/sdk2/authentication#user-handling)
</Warning>

The **SDK Users API** returns a `{authToken}` which has to be used in the Front-end script (a `getAuthToken()` method passed to the `mindset.init()`).

See the doc for more details about [Authentication flow](https://docs.mindset.ai/deploy/sdk3/sdk3-authentication).

## Below an example of an EXPRESS API script

```javascript theme={null}
import express from "express";
import axios from "axios";

const YOUR-MINDSET-API-KEY = 'YOUR-MINDSET-API-KEY' 
const MINDSET-API-HOST = 'MINDSET-API-HOST'
const YOUR-APP-UID = 'YOUR-APP-UID'

const app = express();

app.get("/api/getusertoken", async (req, res) => {
  const { currentUserId } = getcurrentuser(); // A method to get the current user ID from your session or database
  const externalId = "userx123456" // You can use the currentUserId to get the externalId or userEmail from your database or session store

  try {
    const embedUserToken: any = await axios.post(
      `https://${MINDSET-API-HOST}/api/v1/appuid/${YOUR-APP-UID}/sdkusers/auth`,
      {
        name: "My User Name", // optional 
        userEmail: "myuser@email.com", // Pass the userEmail if you want to authenticate a user by email as you did previously
        // OR
        externalId: externalId // Pass the externalId if you want to authenticate a user by externalId
      },
      {
        headers: {
            'Content-Type': 'application/json',
            'x-api-key': YOUR-MINDSET-API-KEY
        },
      }
    )
    res.send(embedUserToken.data);
    
  } catch (error: any) {
    console.error(error);
    console.error("Error", error, error.response.status, error.response, error);
    res.status(500).send("Error getting token");
  };
});

export default app;
```

## Parameters required for your configuration

| Parameter                | Description                                                                                                       |
| :----------------------- | :---------------------------------------------------------------------------------------------------------------- |
| **MINDSET-API-HOST**     | This URL is provided by the Mindset team.                                                                         |
| **YOUR-MINDSET-API-KEY** | This API KEY can be generated in your Mindset App Admin portal (*CONFIGURE* > *API Management* > *Generate key*). |
