tokens
Create a service token
Create a service token for machine-to-machine authentication. Accepts API key or bearer token. Optionally apply restrictions.
POST
/
tokens
JavaScript
import Smithery from '@smithery/api';
const client = new Smithery({
apiKey: process.env['SMITHERY_API_KEY'], // This is the default and can be omitted
});
const createTokenResponse = await client.tokens.create();
console.log(createTokenResponse.token);curl --request POST \
--url https://api.smithery.ai/tokens \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"policy": [
{
"namespaces": [
"my-app"
],
"resources": [
"connections"
],
"operations": [
"read"
],
"metadata": [
{
"userId": "alice"
}
],
"ttl": "1h",
"rpcReqMatch": {
"params.name": "^(create_issue|search_issues)$"
}
}
],
"organizationId": "org_01H1234567890"
}
'import requests
url = "https://api.smithery.ai/tokens"
payload = {
"policy": [
{
"namespaces": ["my-app"],
"resources": ["connections"],
"operations": ["read"],
"metadata": [{ "userId": "alice" }],
"ttl": "1h",
"rpcReqMatch": { "params.name": "^(create_issue|search_issues)$" }
}
],
"organizationId": "org_01H1234567890"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.smithery.ai/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([
'policy' => [
[
'namespaces' => [
'my-app'
],
'resources' => [
'connections'
],
'operations' => [
'read'
],
'metadata' => [
[
'userId' => 'alice'
]
],
'ttl' => '1h',
'rpcReqMatch' => [
'params.name' => '^(create_issue|search_issues)$'
]
]
],
'organizationId' => 'org_01H1234567890'
]),
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://api.smithery.ai/tokens"
payload := strings.NewReader("{\n \"policy\": [\n {\n \"namespaces\": [\n \"my-app\"\n ],\n \"resources\": [\n \"connections\"\n ],\n \"operations\": [\n \"read\"\n ],\n \"metadata\": [\n {\n \"userId\": \"alice\"\n }\n ],\n \"ttl\": \"1h\",\n \"rpcReqMatch\": {\n \"params.name\": \"^(create_issue|search_issues)$\"\n }\n }\n ],\n \"organizationId\": \"org_01H1234567890\"\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://api.smithery.ai/tokens")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"policy\": [\n {\n \"namespaces\": [\n \"my-app\"\n ],\n \"resources\": [\n \"connections\"\n ],\n \"operations\": [\n \"read\"\n ],\n \"metadata\": [\n {\n \"userId\": \"alice\"\n }\n ],\n \"ttl\": \"1h\",\n \"rpcReqMatch\": {\n \"params.name\": \"^(create_issue|search_issues)$\"\n }\n }\n ],\n \"organizationId\": \"org_01H1234567890\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.smithery.ai/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 \"policy\": [\n {\n \"namespaces\": [\n \"my-app\"\n ],\n \"resources\": [\n \"connections\"\n ],\n \"operations\": [\n \"read\"\n ],\n \"metadata\": [\n {\n \"userId\": \"alice\"\n }\n ],\n \"ttl\": \"1h\",\n \"rpcReqMatch\": {\n \"params.name\": \"^(create_issue|search_issues)$\"\n }\n }\n ],\n \"organizationId\": \"org_01H1234567890\"\n}"
response = http.request(request)
puts response.read_body{
"token": "<string>",
"expiresAt": "2024-01-01T01:00:00.000Z"
}{
"error": "unauthorized",
"message": "Invalid API key"
}{
"error": "unauthorized",
"message": "Invalid API key"
}{
"error": "unauthorized",
"message": "Invalid API key"
}Authorizations
Smithery API key as Bearer token
Body
application/json
Constraint objects to restrict the token. Each constraint may include a ttl field (max 24 hours). Default TTL is 1 hour. Maximum is 24 hours.
Minimum array length:
1Show child attributes
Show child attributes
Optional organization ID to scope the token to. When provided, the token is minted with org context. The authenticated user must be an admin or owner of the organization.
Example:
"org_01H1234567890"
Was this page helpful?
Previous
Get user's namespaces or search namespacesWhen called without query params, returns the authenticated user's namespaces (backwards compatible). When query params are provided, searches public namespaces with pagination. Use ownerId to filter by owner, hasServers/hasSkills to filter by content, q for text search.
Next
⌘I
JavaScript
import Smithery from '@smithery/api';
const client = new Smithery({
apiKey: process.env['SMITHERY_API_KEY'], // This is the default and can be omitted
});
const createTokenResponse = await client.tokens.create();
console.log(createTokenResponse.token);curl --request POST \
--url https://api.smithery.ai/tokens \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"policy": [
{
"namespaces": [
"my-app"
],
"resources": [
"connections"
],
"operations": [
"read"
],
"metadata": [
{
"userId": "alice"
}
],
"ttl": "1h",
"rpcReqMatch": {
"params.name": "^(create_issue|search_issues)$"
}
}
],
"organizationId": "org_01H1234567890"
}
'import requests
url = "https://api.smithery.ai/tokens"
payload = {
"policy": [
{
"namespaces": ["my-app"],
"resources": ["connections"],
"operations": ["read"],
"metadata": [{ "userId": "alice" }],
"ttl": "1h",
"rpcReqMatch": { "params.name": "^(create_issue|search_issues)$" }
}
],
"organizationId": "org_01H1234567890"
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.smithery.ai/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([
'policy' => [
[
'namespaces' => [
'my-app'
],
'resources' => [
'connections'
],
'operations' => [
'read'
],
'metadata' => [
[
'userId' => 'alice'
]
],
'ttl' => '1h',
'rpcReqMatch' => [
'params.name' => '^(create_issue|search_issues)$'
]
]
],
'organizationId' => 'org_01H1234567890'
]),
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://api.smithery.ai/tokens"
payload := strings.NewReader("{\n \"policy\": [\n {\n \"namespaces\": [\n \"my-app\"\n ],\n \"resources\": [\n \"connections\"\n ],\n \"operations\": [\n \"read\"\n ],\n \"metadata\": [\n {\n \"userId\": \"alice\"\n }\n ],\n \"ttl\": \"1h\",\n \"rpcReqMatch\": {\n \"params.name\": \"^(create_issue|search_issues)$\"\n }\n }\n ],\n \"organizationId\": \"org_01H1234567890\"\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://api.smithery.ai/tokens")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"policy\": [\n {\n \"namespaces\": [\n \"my-app\"\n ],\n \"resources\": [\n \"connections\"\n ],\n \"operations\": [\n \"read\"\n ],\n \"metadata\": [\n {\n \"userId\": \"alice\"\n }\n ],\n \"ttl\": \"1h\",\n \"rpcReqMatch\": {\n \"params.name\": \"^(create_issue|search_issues)$\"\n }\n }\n ],\n \"organizationId\": \"org_01H1234567890\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.smithery.ai/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 \"policy\": [\n {\n \"namespaces\": [\n \"my-app\"\n ],\n \"resources\": [\n \"connections\"\n ],\n \"operations\": [\n \"read\"\n ],\n \"metadata\": [\n {\n \"userId\": \"alice\"\n }\n ],\n \"ttl\": \"1h\",\n \"rpcReqMatch\": {\n \"params.name\": \"^(create_issue|search_issues)$\"\n }\n }\n ],\n \"organizationId\": \"org_01H1234567890\"\n}"
response = http.request(request)
puts response.read_body{
"token": "<string>",
"expiresAt": "2024-01-01T01:00:00.000Z"
}{
"error": "unauthorized",
"message": "Invalid API key"
}{
"error": "unauthorized",
"message": "Invalid API key"
}{
"error": "unauthorized",
"message": "Invalid API key"
}