# Agent Authentication

Developing AI agents that securely communicate with external applications for users is challenging due to the complexity of managing different auth mechanisms like OAuth, API Key, Password based auth etc.

Let say, if you want to fetch about Solana from web, create a good content from that data and post it in twitter and reddit, all this using AI agents then this is very complex process as every apps have there own requires different authentication - OAuth, API Keys, etc. and the agent needs to work perfectly on you/your user's behalf. Seems an nightmare right??

Not any more, as this is where Vity Toolkit shines :wink:!!

* Manage **user-level authentication**.
* Works with multiple authentication methods.
* Supports frameworks like **Langchain, OpenAI, VercelAI (coming soon)**.
* Securely encrypted authentication data, **powered by Lit Protocol**, ensures that only the legitimate account owner can access and use the tool.
* Store data on your preferred platform, such as **IPFS, AWS**, or other options of your choice.

We take care of all the auth requirements for the app so that you can concentrate on developing AI agents that can transform the world :rocket:.

Now that you understand how valuable Agent Authentication can be, let's explore how you can implement it. *(Spoiler alert: It's super easy!)*

## Integration

This is **an one-time process** were you need authenticate your application/program. For that you usually need to provide client/consumer id and secret.&#x20;

Here we are showing you an example, how you can integrate `REDDIT` app. I assume that you already&#x20;

* Have [created an app in reddit dashboard](https://www.reddit.com/prefs/apps).
* Have [got your API keys from pinata dashboard](https://app.pinata.cloud/developers/api-keys).
* Installed `vity-toolkit` package.

{% stepper %}
{% step %}

### Create a typescript file and open it in vs code

```bash
touch integration.ts
```

{% endstep %}

{% step %}

### Paste this code and give a wallet's private key in toolkit

```typescript
import { App, LangchainToolkit } from 'vity-toolkit';
import { StorageProvider } from 'vity-toolkit/src/storage-providers';

const toolkit = new LangchainToolkit({
    appPrivateKey: "<wallet-private-key>", // PASTE A WALLET'S PRIVATE KEY
    storageProvider: StorageProvider.PINATA
});


// 1. First get the expected params for the integration
// Get the expected params for the integration and fill in the required values
// const expectedParams = toolkit.getExpectedParamsForIntegration({ app: App.REDDIT });
// console.log(expectedParams);

// You will get this params from the console log
const params = {
    CLIENT_ID: "",
    CLIENT_SECRET: "",
}

params.CLIENT_ID = process.env.REDDIT_CLIENT_ID!;
params.CLIENT_SECRET = process.env.REDDIT_CLIENT_SECRET!;


// 2. Check if the integration already exists, pass the params to the isIntegration method
const iDetails = await toolkit.isIntegration({ app: App.REDDIT }); // The app you want to integrate

if (iDetails.success) {
    console.log("Integration already exists!");
} else {
    // Initiate the integration
    await toolkit.appIntegration({
        app: App.REDDIT,
        authData: params,
    })
}
```

> Note: Make sure to have some [solana devnet tokens](https://faucet.solana.com/) in the wallet.
> {% endstep %}

{% step %}

### Export the environmental variables

Open terminal and paste this there. Fill all the required variables you got from reddit and pinata dashboard.

<pre class="language-bash"><code class="lang-bash">export REDDIT_CLIENT_ID=
export REDDIT_CLIENT_SECRET=
<strong>export PINATA_JWT=
</strong>export PINATA_GATEWAY=
</code></pre>

{% endstep %}

{% step %}

### Execute the file

```bash
npm run integration.ts
```

{% endstep %}
{% endstepper %}

After doing this your app will be integrated. Now let's see how you/your users can connect with your app.

## Connection

This is responsible for authenticating you or your users with your app. This process must be completed for each new user.

Here we are showing you an example, how you can setup for you/your user to connect with your integrated `REDDIT` app. I assume that you already&#x20;

* Have an reddit account.

{% stepper %}
{% step %}

### Create a typescript file and open it in vs code

```bash
touch connection.ts
```

{% endstep %}

{% step %}

### Paste this code and give required wallet's private key in toolkit

```typescript
import { App, LangchainToolkit } from 'vity-toolkit';
import { AuthType } from 'vity-toolkit/src/sdk/types';
import { StorageProvider } from 'vity-toolkit/src/storage-providers';


const toolkit = new LangchainToolkit({
    appPrivateKey: "<app-wallet-private-key>", // This should be same private key which you gave in INTEGRATION process
    userPrivateKey: "<user-wallet-private-key>",
    storageProvider: StorageProvider.PINATA
});


// 1. First get the expected params for the integration
// Get the expected params for the integration and fill in the required values
// const expectedParams = toolkit.getExpectedParamsForConnection({ app: App.REDDIT, type: AuthType.PASSWORD_BASED_AUTH });
// console.log(expectedParams);

// You will get this params from the console log
const params = {
    USERNAME: "",
    PASSWORD: "",
}

params.USERNAME = process.env.REDDIT_USERNAME!;
params.PASSWORD = process.env.REDDIT_PASSWORD!;


// 2. Check if the integration already exists, pass the params to the isIntegration method
const iDetails = await toolkit.isConnection({ app: App.REDDIT }); // The app you want to integrate

if (iDetails.success) {
    console.log("Connection already exists!");
} else {
    // Initiate the integration
    await toolkit.initiateAppConnection({
        app: App.REDDIT,
        type: AuthType.PASSWORD_BASED_AUTH,
        authData: params,
    })
}
```

> Note: Make sure to have some [solana devnet tokens](https://faucet.solana.com/) in both the wallets.
> {% endstep %}

{% step %}

### Export the environmental variables

Open terminal and paste this there. Fill all the required variables you got from pinata dashboard.

```bash
export PINATA_JWT=
export PINATA_GATEWAY=
```

{% endstep %}

{% step %}

### Execute the file

```bash
npm run connection.ts
```

{% endstep %}
{% endstepper %}

Congratulations, you have completed the Authentication process :partying\_face:. Now your AI Agent can use the REDDIT app on behalf of you/your user.&#x20;

You can run this code to check.

```typescript
import { VityToolKit, Action } from "vity-toolkit";


const toolkit = new VityToolKit({
    appPrivateKey: "<app-wallet-private-key>",
    userPrivateKey: "<user-wallet-private-key>",
});

// 1. Get expected params for an action
// const expectedParams = toolKit.getInputParamsForAction({ action: Action.REDDIT_FILTER });
// console.log(expectedParams);

// 2. Get expected params for executing the action
const params = {
    subreddit: "python",
    query: ["fastapi"],
    sort: "new",
    limit: 1,
    time: "all",
}

const result = await toolKit.executeAction({ action: Action.REDDIT_FILTER, inputParams: params });
console.log(result.data);
```


---

# 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://vity-toolkit.gitbook.io/vity-toolkit/features/agent-authentication.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.
