Back to Smithery

Create a service token

docs-api-reference-tokens-create-a-service-token.md

latest11.1 KB
Original Source

POST

/

tokens

Try it

JavaScript

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

200

400

401

403

{
  "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

[​

](#authorization-authorization)

Authorization

string

header

required

Smithery API key as Bearer token

Body

application/json

[​

](#body-policy)

policy

object[]

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: 1

Show child attributes

[​

](#body-organization-id)

organizationId

string

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"

Response

200

application/json

Token created successfully

[​

](#response-token)

token

string

required

The signed service token.

[​

](#response-expires-at)

expiresAt

string

required

ISO 8601 timestamp when the token expires.

Example:

"2024-01-01T01:00:00.000Z"

Was this page helpful?

YesNo

⌘I

JavaScript

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

200

400

401

403

{
  "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"
}