> For a complete index of all SignalWire documentation pages, fetch https://signalwire.com/docs/llms.txt

# createToken

> Chat API namespace — token creation.

Chat API namespace — token creation.

### Schema

```yaml
openapi: 3.1.0
info:
  title: API
  version: 1.0.0
paths:
  /api/chat/tokens:
    post:
      operationId: create_chat_token
      summary: Create chat token
      description: >-
        Generate a Chat Token to be used to authenticate clients to the Chat
        Service.


        #### Permissions


        The API token used to authenticate must have the following scope(s)
        enabled to make a successful request: _Chat_.


        [Learn more about API scopes](/docs/platform/your-signalwire-api-space).
      tags:
        - subpackage_chatTokens
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Chat.ChatToken'
        '400':
          description: The request is invalid.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Types.StatusCodes.StatusCode400'
        '401':
          description: Access is unauthorized.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Types.StatusCodes.StatusCode401'
        '422':
          description: The request contains invalid parameters. See errors for details.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Chat.ChatToken422Error'
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Chat.NewChatToken'
servers:
  - url: https://%7BYour_Space_Name%7D.signalwire.com
    description: SignalWire API
components:
  schemas:
    Chat.ChatChannel:
      type: object
      properties: {}
      description: >-
        User-defined channel names. Each channel is an object with `read` and/or
        `write` properties.

        Max of 500 channels. Either `read`, `write`, or both are required inside
        each channel and default to `false`.

        Each channel name can be up to 250 characters. Channel names cannot
        start with the reserved prefix `sw_`.

        Must be valid JSON.
      title: Chat.ChatChannel
    Chat.ChatState:
      type: object
      properties: {}
      description: >-
        An arbitrary JSON object available to store stateful application
        information in. Must be valid JSON and have a maximum size of 2,000
        characters.
      title: Chat.ChatState
    Chat.NewChatToken:
      type: object
      properties:
        ttl:
          type: integer
          description: >-
            The maximum time, in minutes, that the access token will be valid
            for. Between 1 and 43,200 (30 days).
        channels:
          $ref: '#/components/schemas/Chat.ChatChannel'
          description: >-
            User-defined channel names with read/write permissions. Max of 500
            channels. Channel names cannot start with the reserved prefix `sw_`
            and can be up to 250 characters.
        member_id:
          type: string
          description: >-
            The unique identifier of the member. Up to 250 characters. If not
            specified, a random UUID will be generated.
        state:
          $ref: '#/components/schemas/Chat.ChatState'
          default: {}
          description: >-
            An arbitrary JSON object available to store stateful application
            information in. Must be valid JSON and have a maximum size of 2,000
            characters.
      required:
        - ttl
        - channels
      title: Chat.NewChatToken
    Chat.ChatToken:
      type: object
      properties:
        token:
          type: string
          description: The generated Chat Token.
      required:
        - token
      title: Chat.ChatToken
    TypesStatusCodesStatusCode400Error:
      type: string
      enum:
        - Bad Request
      title: TypesStatusCodesStatusCode400Error
    Types.StatusCodes.StatusCode400:
      type: object
      properties:
        error:
          $ref: '#/components/schemas/TypesStatusCodesStatusCode400Error'
      required:
        - error
      description: The request is invalid.
      title: Types.StatusCodes.StatusCode400
    TypesStatusCodesStatusCode401Error:
      type: string
      enum:
        - Unauthorized
      title: TypesStatusCodesStatusCode401Error
    Types.StatusCodes.StatusCode401:
      type: object
      properties:
        error:
          $ref: '#/components/schemas/TypesStatusCodesStatusCode401Error'
      required:
        - error
      description: Access is unauthorized.
      title: Types.StatusCodes.StatusCode401
    Types.StatusCodes.RestApiErrorItem:
      type: object
      properties:
        type:
          type: string
          description: The category of error.
        code:
          type: string
          description: A specific error code.
        message:
          type: string
          description: A description of what caused the error.
        attribute:
          type:
            - string
            - 'null'
          description: The request parameter that caused the error, if applicable.
        url:
          type: string
          description: A link to documentation about this error.
      required:
        - type
        - code
        - message
        - url
      description: Details about a specific error.
      title: Types.StatusCodes.RestApiErrorItem
    Chat.ChatToken422Error:
      type: object
      properties:
        errors:
          type: array
          items:
            $ref: '#/components/schemas/Types.StatusCodes.RestApiErrorItem'
          description: List of validation errors.
      required:
        - errors
      description: The request contains invalid parameters. See errors for details.
      title: Chat.ChatToken422Error

```

## **Response Example**

### Response (200)

```json
{
  "token": "eyJ0eXAiOiJWUlQiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE2MjIxMjAxMjMsI...wMCwicnNlIjo5MDB9-BqG-DqC5LhpsdMWEFjhVkTBpQ"
}
```

## **Example**

```typescript {4}
import { RestClient } from "@signalwire/sdk";

const client = new RestClient();
const result = await client.chat.createToken({ channels: ['general'] });
console.log(result);
```