curl --request POST \
--url https://{host}/v1/session-tokens \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"origins": [
"https://app.example.com"
],
"subject": "user-42",
"ttlSeconds": 900
}
'import requests
url = "https://{host}/v1/session-tokens"
payload = {
"origins": ["https://app.example.com"],
"subject": "user-42",
"ttlSeconds": 900
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({origins: ['https://app.example.com'], subject: 'user-42', ttlSeconds: 900})
};
fetch('https://{host}/v1/session-tokens', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://{host}/v1/session-tokens",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'origins' => [
'https://app.example.com'
],
'subject' => 'user-42',
'ttlSeconds' => 900
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://{host}/v1/session-tokens"
payload := strings.NewReader("{\n \"origins\": [\n \"https://app.example.com\"\n ],\n \"subject\": \"user-42\",\n \"ttlSeconds\": 900\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://{host}/v1/session-tokens")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"origins\": [\n \"https://app.example.com\"\n ],\n \"subject\": \"user-42\",\n \"ttlSeconds\": 900\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://{host}/v1/session-tokens")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"origins\": [\n \"https://app.example.com\"\n ],\n \"subject\": \"user-42\",\n \"ttlSeconds\": 900\n}"
response = http.request(request)
puts response.read_body{
"expiresAt": 123,
"token": "<string>"
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}Mint a session token for a browser or app user.
A backend calls this with its API key, once per signed-in user, and hands the token to the page. The page then calls the API directly, from the named origins, until the token expires. Nothing refreshes a token, so mint the next one before the current one expires. A token spends its key’s credits within its key’s limits. Revoking the key ends every token it minted.
curl --request POST \
--url https://{host}/v1/session-tokens \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"origins": [
"https://app.example.com"
],
"subject": "user-42",
"ttlSeconds": 900
}
'import requests
url = "https://{host}/v1/session-tokens"
payload = {
"origins": ["https://app.example.com"],
"subject": "user-42",
"ttlSeconds": 900
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({origins: ['https://app.example.com'], subject: 'user-42', ttlSeconds: 900})
};
fetch('https://{host}/v1/session-tokens', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://{host}/v1/session-tokens",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'origins' => [
'https://app.example.com'
],
'subject' => 'user-42',
'ttlSeconds' => 900
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://{host}/v1/session-tokens"
payload := strings.NewReader("{\n \"origins\": [\n \"https://app.example.com\"\n ],\n \"subject\": \"user-42\",\n \"ttlSeconds\": 900\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://{host}/v1/session-tokens")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"origins\": [\n \"https://app.example.com\"\n ],\n \"subject\": \"user-42\",\n \"ttlSeconds\": 900\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://{host}/v1/session-tokens")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"origins\": [\n \"https://app.example.com\"\n ],\n \"subject\": \"user-42\",\n \"ttlSeconds\": 900\n}"
response = http.request(request)
puts response.read_body{
"expiresAt": 123,
"token": "<string>"
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}{
"error": {
"code": "UNAUTHORIZED",
"message": "<string>",
"details": "<unknown>"
}
}Authorizations
Send an API key, or a session token a key minted, as Authorization: Bearer <credential>. A browser opening /v1/stream offers its session token in Sec-WebSocket-Protocol instead.
Body
The body of POST /v1/session-tokens.
The origins the token may be presented from, 1 to 10, each as a browser sends Origin.
1 - 10 elements["https://app.example.com"]
Who the token is for, in the caller's own terms: at most 128 characters, no control characters.
128"user-42"
How long the token lives, 60 to 3600 seconds; 900 when absent.
60 <= x <= 3600900
Response
The token, and when it expires. Present it as Authorization: Bearer <token>, or on the stream handshake in Sec-WebSocket-Protocol beside metastreams.v1, from one of its origins.