SaaS版 クイックスタート
Client Credentials 認証でトークンを取得し、SSE 経由でエージェントと会話するまでの手順を解説します。
はじめに
SaaS版の API を利用するための最短ステップを紹介します。以下の3つのステップでエージェントとの会話を開始できます。
前提条件
- APIオプションの申込が完了していること
- Console に Admin 管理者がログインできること
- Console で Client Credentials タイプのクライアント(アプリケーション)を作成済みであること(Console セットアップガイド)
- 以下の情報を取得済みであること:
Client ID- 作成したクライアントの IDClient Secret- クライアントのシークレット(機密情報)
Step 1: 認証情報の確認
API 利用前に、以下の情報が揃っていることを確認してください。これらは管理コンソールで確認できます。
準備チェックリスト
- APIオプションの申込が完了していること
- Console に Admin 管理者がログインできること
- Console で Client Credentials タイプのクライアント(アプリケーション)を作成済みであること
- 以下の情報を取得済みであること:
Client ID- 作成したクライアントの IDClient Secret- クライアントのシークレット(機密情報)
例
Client ID: saas-app-001
Client Secret: (hidden)
Step 2: トークン取得
Client Credentials フロー を使用して、アクセストークンを取得します。サーバー側で以下のリクエストを実行してください。
リクエスト例
- curl
- Python
- TypeScript
- Go
curl
curl -X POST https://auth.agenticstar.jp/federation/auth/v1/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials" \
-d "client_id=<your-client-id>" \
-d "client_secret=<your-client-secret>"
Python
import requests
response = requests.post(
"https://auth.agenticstar.jp/federation/auth/v1/token",
data={
"grant_type": "client_credentials",
"client_id": "<your-client-id>",
"client_secret": "<your-client-secret>",
},
)
token = response.json()["access_token"]
print(token)
TypeScript
const response = await fetch(
"https://auth.agenticstar.jp/federation/auth/v1/token",
{
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
grant_type: "client_credentials",
client_id: "<your-client-id>",
client_secret: "<your-client-secret>",
}),
}
);
const { access_token } = await response.json();
console.log(access_token);
Go
package main
import (
"fmt"
"net/http"
"net/url"
"encoding/json"
)
func main() {
resp, _ := http.PostForm(
"https://auth.agenticstar.jp/federation/auth/v1/token",
url.Values{
"grant_type": {"client_credentials"},
"client_id": {"<your-client-id>"},
"client_secret": {"<your-client-secret>"},
},
)
defer resp.Body.Close()
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
fmt.Println(result["access_token"])
}
パラメータ
<your-client-id>: Step 1で確認したクライアント ID<your-client-secret>: Step 1で確認したシークレット
レスポンス例
JSON
{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "Bearer",
"expires_in": 3600
}
トークンの有効期限
expires_in に指定された秒数の間、トークンが有効です。期限切れ前に新しいトークンを取得してください。Client Credentials フローではリフレッシュトークンは発行されないため、期限切れ後は同じリクエストを再度実行してください。Authorization Code フローを使いたい場合は認証ガイドを参照してください。
Step 3: Chat API 呼び出し
取得したアクセストークンを使用して、SSE(Server-Sent Events)経由で Chat API を呼び出します。ストリーム形式でエージェントからのリアルタイム応答を受け取ります。
リクエスト例
- curl
- Python
- TypeScript
- Go
curl
curl -X POST https://api.agenticstar.jp/v1/chat/completions \
-H "Authorization: Bearer <access_token>" \
-H "Content-Type: application/json" \
-d '{
"model": "AGENTIC STAR",
"stream": true,
"messages": [
{
"role": "user",
"content": "こんにちは"
}
]
}'
Python
import requests
import json
response = requests.post(
"https://api.agenticstar.jp/v1/chat/completions",
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
},
json={
"model": "AGENTIC STAR",
"stream": True,
"messages": [{"role": "user", "content": "こんにちは"}],
},
stream=True,
)
for line in response.iter_lines():
if line:
data = line.decode("utf-8")
if data == "data: [DONE]":
break
if data.startswith("data: "):
chunk = json.loads(data[6:])
content = chunk["choices"][0]["delta"].get("content", "")
print(content, end="", flush=True)
TypeScript
const response = await fetch(
"https://api.agenticstar.jp/v1/chat/completions",
{
method: "POST",
headers: {
"Authorization": `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "AGENTIC STAR",
stream: true,
messages: [{ role: "user", content: "こんにちは" }],
}),
}
);
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const text = decoder.decode(value);
for (const line of text.split("\n")) {
if (line.startsWith("data: ") && line !== "data: [DONE]") {
const chunk = JSON.parse(line.slice(6));
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
}
}
Go
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
)
func main() {
body, _ := json.Marshal(map[string]interface{}{
"model": "AGENTIC STAR",
"stream": true,
"messages": []map[string]string{
{"role": "user", "content": "こんにちは"},
},
})
req, _ := http.NewRequest("POST",
"https://api.agenticstar.jp/v1/chat/completions",
bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "data: ") {
data := line[6:]
if data == "[DONE]" { break }
var chunk map[string]interface{}
json.Unmarshal([]byte(data), &chunk)
fmt.Print(chunk["choices"])
}
}
}
パラメータ
<access_token>: Step 2で取得したアクセストークンstream: true: SSE ストリーミングを有効化messages: ユーザーからのメッセージ。配列形式で複数メッセージも可能
レスポンス例(SSE ストリーム)
data: {"choices":[{"delta":{"content":"こんにちは"}}]}
data: {"choices":[{"delta":{"content":"、お手伝い"}}]}
data: {"choices":[{"delta":{"content":"できることはありますか"}}]}
data: [DONE]
SSE ストリーミングについて
レスポンスは Server-Sent Events(SSE)形式で、チャンク単位でリアルタイムに送信されます。各チャンクは data: JSON 形式です。data: [DONE] で会話が完了します。詳細は ストリーミングガイド を参照してください。
次のステップ
さらに詳しい情報と実装パターンについて、以下のドキュメントを参照してください。