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

# create_token

> Create a PubSub authentication token.

Create a PubSub authentication token.

### Schema

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


        #### Permissions


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


        [Learn more about API scopes](/docs/platform/your-signalwire-api-space).
      tags:
        - subpackage_pubSubTokens
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PubSub.PubSubToken'
        '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/PubSub.PubSubToken422Error'
        '500':
          description: An internal server error occurred.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Types.StatusCodes.StatusCode500'
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PubSub.NewPubSubToken'
servers:
  - url: https://%7BYour_Space_Name%7D.signalwire.com
    description: SignalWire API
components:
  schemas:
    PubSub.PubSubChannels:
      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. Must be valid JSON.
      title: PubSub.PubSubChannels
    PubSub.PubSubState:
      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: PubSub.PubSubState
    PubSub.NewPubSubToken:
      type: object
      properties:
        ttl:
          type: integer
          description: >-
            The maximum time, in minutes, for which the access token will be
            valid. Between 1 and 43,200 (30 days).
        channels:
          $ref: '#/components/schemas/PubSub.PubSubChannels'
          description: >-
            Each channel with `write` and `read` objects with boolean as values.
            Max of 500 channels inside main `channels`.

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

            Each channel name can be up to 250 characters. Must be valid JSON.
        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/PubSub.PubSubState'
          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: PubSub.NewPubSubToken
    PubSub.PubSubToken:
      type: object
      properties:
        token:
          type: string
          description: >-
            A PubSub Token to be used to authenticate clients to the PubSub
            Service.
      required:
        - token
      title: PubSub.PubSubToken
    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
    PubSub.PubSubToken422Error:
      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: PubSub.PubSubToken422Error
    TypesStatusCodesStatusCode500Error:
      type: string
      enum:
        - Internal Server Error
      title: TypesStatusCodesStatusCode500Error
    Types.StatusCodes.StatusCode500:
      type: object
      properties:
        error:
          $ref: '#/components/schemas/TypesStatusCodesStatusCode500Error'
      required:
        - error
      description: An internal server error occurred.
      title: Types.StatusCodes.StatusCode500

```

## **Response Example**

### Response (200)

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

## **Example**

```python {9}
from signalwire.rest import RestClient

client = RestClient(
    project="your-project-id",
    token="your-api-token",
    host="your-space.signalwire.com",
)

token = client.pubsub.create_token(
    channels=["notifications", "updates"],
    member_id="user-123",
    ttl=3600,
)
print("PubSub token:", token.get("token"))
```