import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import TOCInline from '@theme/TOCInline';

# Granting Write Permission

<TOCInline
  maxHeadingLevel={4}
  toc={toc}
/>

## Overview

Accounts can grant write permission to other accounts for a set of keys.

## Terminology

* **Grantee**: The account that is being granted write access to the list of keys.
* **Granter**: The account that will grant access to the list of keys.

## Granting an account write permission

<Tabs
  defaultValue="javascript-via-package-manager"
  values={[
    { label: 'JavaScript (via package manager)', value: 'javascript-via-package-manager' },
    { label: 'JavaScript (via CDN)', value: 'javascript-via-cdn' },
    { label: 'TypeScript', value: 'typescript' },
  ]}>
  <TabItem value="javascript-via-package-manager">

    ```js
    const { Social } = require('@builddao/near-social-js');

    const social = new Social();
    const transaction = await social.grantWritePermission({
      account: {
        accountID: 'bob.near',
        publicKey: 'ed25519:H9k5eiU4xXS3M4z8HzKJSLaZdqGdGwBG49o7orNC4eZW',
      },
      granteeAccountId: 'alice.near',
      keys: [
        'alice.near/profile/name',
        'alice.near/profile/image/url',
      ],
    });

    // ...sign the returned transaction and post to the network
    */
    ```

  </TabItem>
  <TabItem value="javascript-via-cdn">

    ```js
    var social = new NEARSocialSDK();

    social.grantWritePermission({
      account: {
        accountID: 'bob.near',
        publicKey: 'ed25519:H9k5eiU4xXS3M4z8HzKJSLaZdqGdGwBG49o7orNC4eZW',
      },
      granteeAccountId: 'alice.near',
      keys: [
        'alice.near/profile/name',
        'alice.near/profile/image/url'
      ]
    })
    .then((transaction) => {
      // ...sign the returned transaction and post to the network
    });
    ```

  </TabItem>
  <TabItem value="typescript">

    ```typescript
    import { Social } from '@builddao/near-social-js';

    const social = new Social();
    const transaction = await social.grantWritePermission({
      account: {
        accountID: 'bob.near',
        publicKey: 'ed25519:H9k5eiU4xXS3M4z8HzKJSLaZdqGdGwBG49o7orNC4eZW',
      },
      granteeAccountId: 'alice.near',
      keys: [
        'alice.near/profile/name',
        'alice.near/profile/image/url',
      ],
    });

    // ...sign the returned transaction and post to the network
    */
    ```

  </TabItem>
</Tabs>

:::caution

If the grantee account ID or the account ID in each key is not a valid account ID then a [`InvalidAccountIdError`](../api-reference/errors#invalidaccountiderror) is thrown.

:::

:::caution

If a key does is not owned by the granter, then a [`KeyNotAllowedError`](../api-reference/errors#keynotallowederror) is thrown.

:::
