---
title: useGenerateUserToken
description: >-
  The useGenerateUserToken hook generates a temporary token for authenticating
  the current user with your backend.
source_url:
  html: 'https://shopify.dev/docs/api/shop-minis/hooks/user/usegenerateusertoken'
  md: 'https://shopify.dev/docs/api/shop-minis/hooks/user/usegenerateusertoken.md'
---

# useGenerateUserToken

The `useGenerateUserToken` hook generates a temporary token for authenticating the current user with your backend. This token can be verified using the [`userTokenVerify`](https://shopify.dev/docs/api/shop-minis/minis-admin-api/mutations/usertokenverify) mutation to obtain a permanent user identifier. See [Verifying requests](https://shopify.dev/docs/api/shop-minis/network-requests#verifying-requests) for implementation details.

**Note:**

Some common use cases are: authenticating API requests to your backend, identifying users for personalized experiences, securely linking Shop users to your application's user database.

**Note:**

This hook optionally uses the following scope(s) when declared in the manifest: `openid`

For more details, see [manifest.json](https://shopify.dev/docs/api/shop-minis/manifest-file).

## use​Generate​User​Token()

### Returns

* **UseGenerateUserTokenReturns**

### UseGenerateUserTokenReturns

* generateUserToken

  Generates a temporary token for the user. Tokens are cached in memory and reused if still valid (with a 5-minute expiry buffer). A new token is automatically generated when the cached token is expired or missing.

  ```ts
  () => Promise<{ data: GeneratedTokenData; userErrors?: UserTokenGenerateUserErrors[]; }>
  ```

### GeneratedTokenData

* expiresAt

  The expiration time of the token.

  ```ts
  ISO8601DateTime | null
  ```

* token

  A temporary token for the user.

  ```ts
  string | null
  ```

* userState

  Whether the user is verified or a guest.

  ```ts
  UserState | null
  ```

### ISO8601DateTime

```ts
string
```

### UserState

* GUEST

  ```ts
  GUEST
  ```

* VERIFIED

  ```ts
  VERIFIED
  ```

### UserTokenGenerateUserErrors

* code

  ```ts
  UserTokenGenerateUserErrorCode
  ```

* field

  ```ts
  string[] | null
  ```

* message

  ```ts
  string
  ```

### UserTokenGenerateUserErrorCode

* MINI\_NOT\_FOUND

  ```ts
  MINI_NOT_FOUND
  ```

* USER\_NOT\_FOUND

  ```ts
  USER_NOT_FOUND
  ```

Examples

### Examples

* ####

  ##### tsx

  ```tsx
  import {useCallback} from 'react'

  import {useGenerateUserToken, Button} from '@shopify/shop-minis-react'

  export default function MyComponent() {
    const {generateUserToken} = useGenerateUserToken()

    const performRequest = useCallback(async () => {
      const {data} = await generateUserToken()
      const {token, expiresAt, userState} = data

      console.log({token, expiresAt, userState})
    }, [generateUserToken])

    return <Button onPress={performRequest} text="Perform request" />
  }
  ```

***
