Web Pub Sub - Generate Client Token

Generate token for the client to connect Azure Web PubSub service.

POST {endpoint}/api/hubs/{hub}/:generateToken?api-version=2024-01-01
POST {endpoint}/api/hubs/{hub}/:generateToken?userId={userId}&role={role}&minutesToExpire={minutesToExpire}&api-version=2024-01-01&group={group}&clientType={clientType}

URI Parameters

Name In Required Type Description
endpoint
path True

string

url

HTTP or HTTPS endpoint for the Web PubSub service instance.

hub
path True

string

Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.

Regex pattern: ^[A-Za-z][A-Za-z0-9_`,.[\]]{0,127}$

api-version
query True

string

The version of the REST APIs.

clientType
query

WebPubSubClientType

The type of client. Case-insensitive. If not set, it's "Default". For Web PubSub for Socket.IO, only the default value is supported. For Web PubSub, the valid values are 'Default' and 'MQTT'.

group
query

string[]

Groups that the connection will join when it connects.

minutesToExpire
query

integer

int32

The expire time of the generated token.

role
query

string[]

Roles that the connection with the generated token will have.

userId
query

string

User Id.

Responses

Name Type Description
200 OK

ClientTokenResponse

Success

Media Types: "text/plain", "application/json", "text/json"

Other Status Codes

ErrorDetail

Error response

Media Types: "text/plain", "application/json", "text/json"

Headers

x-ms-error-code: string

Examples

WebPubSub_GenerateClientToken

Sample request

POST {endpoint}/api/hubs/hub1/:generateToken?userId=user1&minutesToExpire=5&api-version=2024-01-01&clientType=MQTT

Sample response

{
  "token": "ABCDEFG.ABC.ABC"
}

Definitions

Name Description
ClientTokenResponse

The response object containing the token for the client

ErrorDetail

The error object.

InnerError
WebPubSubClientType

The type of client. Case-insensitive. If not set, it's "Default". For Web PubSub for Socket.IO, only the default value is supported. For Web PubSub, the valid values are 'Default' and 'MQTT'.

ClientTokenResponse

The response object containing the token for the client

Name Type Description
token

string

The token value for the WebSocket client to connect to the service

ErrorDetail

The error object.

Name Type Description
code

string

One of a server-defined set of error codes.

details

ErrorDetail[]

An array of details about specific errors that led to this reported error.

inner

InnerError

message

string

A human-readable representation of the error.

target

string

The target of the error.

InnerError

Name Type Description
code

string

A more specific error code than was provided by the containing error.

inner

InnerError

WebPubSubClientType

The type of client. Case-insensitive. If not set, it's "Default". For Web PubSub for Socket.IO, only the default value is supported. For Web PubSub, the valid values are 'Default' and 'MQTT'.

Name Type Description
Default

string

MQTT

string