Whakamotuhēhē
Kei ia tono tō API key i tētahi header. Ka mahi te key kotahi mō ia pūkete i ia endpoint me ia model, i tētahi o ngā āhua header e rua.
Header · ngā SDK OpenAI
Authorization: Bearer YOUR_API_KEY Header · ngā SDK Anthropic
x-api-key: YOUR_API_KEY Ngā header e whakaaetia ana
Ka pānuitia ngā header e rua i ia endpoint /v1, ahakoa te hōputu e kōrero ana te endpoint. Tukuna tētahi o rāua.
| Header | Whakaahuatanga |
|---|---|
Authorization: Bearer <key> | Ka tirohia tuatahi. Tuhia te Bearer me te B tāpiri me tētahi mokowā i mua i te key. Ka tuku ngā SDK OpenAI i tēnei header. |
x-api-key: <key> | Ka pānuitia ina kāore he header Authorization: Bearer i te tono. Ka tuku ngā SDK Anthropic i tēnei header. |
Me tētahi SDK OpenAI, tautuhia te key me te base URL; ka tuku te SDK i te Authorization: Bearer.
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.shannon-ai.com/v1"
)
response = client.chat.completions.create(
model="shannon-3",
messages=[
{"role": "user", "content": "Hello, Shannon!"}
]
)
print(response.choices[0].message.content) import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'YOUR_API_KEY',
baseURL: 'https://api.shannon-ai.com/v1'
});
const response = await client.chat.completions.create({
model: 'shannon-3',
messages: [
{ role: 'user', content: 'Hello, Shannon!' }
]
});
console.log(response.choices[0].message.content); curl -X POST "https://api.shannon-ai.com/v1/chat/completions" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "shannon-3",
"messages": [
{"role": "user", "content": "Hello, Shannon!"}
]
}' Me tētahi SDK Anthropic, tautuhia te key me te base URL kāore he /v1; ka tuku te SDK i te x-api-key.
import anthropic
client = anthropic.Anthropic(
api_key="YOUR_API_KEY",
base_url="https://api.shannon-ai.com"
)
message = client.messages.create(
model="shannon-3",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, Shannon!"}
]
)
print("".join(block.text for block in message.content if block.type == "text")) import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: 'YOUR_API_KEY',
baseURL: 'https://api.shannon-ai.com'
});
const message = await client.messages.create({
model: 'shannon-3',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Hello, Shannon!' }
]
});
console.log(message.content.filter((block) => block.type === 'text').map((block) => block.text).join('')); curl -X POST "https://api.shannon-ai.com/v1/messages" \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "shannon-3",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Hello, Shannon!"}
]
}' Ngā endpoint e hiahia ana ki te key
| Endpoint | Key |
|---|---|
POST /v1/chat/completions | Hiahiatia |
POST /v1/messages | Hiahiatia |
POST /v1/responses | Hiahiatia |
POST /v1/tokenize | Hiahiatia |
POST /v1/messages/count_tokens | Hiahiatia |
GET /v1/models | Kāore e hiahiatia |
Tō key
- He key te
sk-ka whai i te 43 tohu mai i teA–Z,a–z,0–9,-me_: 46 tohu katoa. - Kotahi tonu te key o ia pūkete. Ka hangaia i te wā tuatahi ka huaki koe i Keys & usage.
- Ka hoatu e te key te tomo katoa ki te API mō tō pūkete: ia endpoint, ia model me te toenga katoa.
- Kāore he rā mutunga o tētahi key. Ka mahi tae noa ki te wā ka huri, ka whakakore rānei koe.
Huri me te whakakore
Ka huri, ka whakakore koe i te key i te whārangi Keys & usage. Keys & usage
| Mahi | Ka aha |
|---|---|
| Huri | Ka tukuna tētahi key hōu, ā, ka mutu te mahi a te key tawhito. Ka mahi tonu te key hōu. |
| Whakakore | Ka mutu te mahi a te key, ā, kāore he key hōu e tukuna. Hurihia kia whiwhi anō i tētahi key mahi. |
- Ka kaha tahi rāua: ka whakautua te tono e whai ake me te key tawhito ki te
401. Whakaarohia te 60 hekona hei tepe o runga i mua i te whakakorenga o te key tawhito ki ngā wāhi katoa. - Kotahi te key e whakamahia ana e ia kiritaki o te pūkete, nō reira ka tae te huri ki a rātou katoa. Whakatōngia te key hōu ki ō kiritaki i muri tonu i te huri.
- Nō te pūkete te toenga, te whakamahinga me te flood protection, ehara nō te key. Ka haere tonu te key hōu me te toenga ōrite me te tatau tono ōrite mō te meneti o nāianei.
Ngā hapa
Ko ia rahua whakamotuhēhē he tūnga 401 me te momo authentication_error. Mā te karere e wehe ngā take.
| Tūnga | Momo | Karere | Āhea |
|---|---|---|---|
401 | authentication_error | Missing authentication | Kāore he header Authorization: Bearer i te tono, kāore hoki he header x-api-key e tīmata ana ki te sk-. Koinei hoki te whakautu mō te bearer reta iti me tētahi uara Authorization kāore he Bearer. |
401 | authentication_error | Invalid API key | Ehara te key i te key o nāianei o tētahi pūkete: he hē te patapatai, kua whakakorea rānei, kua whakakapia rānei mā te huri. |
{
"error": {
"type": "authentication_error",
"message": "Invalid API key",
"code": "invalid_api_key",
"param": null
}
} {
"type": "error",
"error": {
"type": "authentication_error",
"message": "Invalid API key"
}
} - I ia ara haunga ko
/v1/messages, kei teerrorobject hoki tecode: "invalid_api_key", mō te key ngaro hoki, me teparam: null. - Ka tirohia te key i mua i te pānuitanga o te tinana. Ka whiwhi te tono kāore he key whai mana i te
401ahakoa he muhu te tinana, he nui rawa rānei. - Kāore te uara
Authorization: Bearere tīmata ana ki tesk-e whakaarotia hei API key. Ka whiwhi i te401me tētahi karere kei te muhu, kua pau rānei te token.
Ngā pūtirotiro me CORS
- Ka whakautu te API ki ngā tono pūtirotiro cross-origin mō tētahi rārangi origin kua tautuhia anake. Kāore e taea e te JavaScript i runga i tētahi whārangi o tētahi atu origin te pānui i ngā whakautu
/v1. - Karangatia te API mai i tō server ka tukua tō whārangi kia kōrero ki tō server. Ka puritia hoki te key ki waho i te puna o te whārangi.
- Kāore e pāngia te code ā-server me ngā taputapu rārangi whakahau: ko ngā pūtirotiro anake e whakamahi ana i tēnei ture.
- Kāore te takiuru i shannon-ai.com e whakamotuhēhē i ngā tono
/v1. Kāore ngā cookies e pānuitia ki reira; ko te key anake te taipitopito whakamotuhēhē. - Mō tētahi origin kua whakaaetia, ka taea e te pūtirotiro te tuku i te
content-type,authorization,x-api-key,anthropic-version,anthropic-beta,openai-betame tex-request-id, ā, ka taea te pānui i te header whakautux-request-id.
Te tiaki i te key
- Pupurihia te key ki tētahi server. Kaua e tāpiri ki ngā whārangi tukutuku, ngā taupānga pūkoro, ngā repository tūmatanui rānei: ka taea e te tangata kei a ia te whakapau i tō toenga katoa.
- Utaina te key mai i tētahi environment variable, i tētahi secret store rānei, kaua e tuhi ki roto i te code.
- Mēnā kua pīhoihoi pea te key, hurihia i Keys & usage. Ka mutu tonu te mahi a te key tawhito.
- I muri i te pīhoihoi, pānuitia te rārangi tono i Keys & usage. Ka whakaatu i ia tono i mahia e te key i ngā rā 30 kua pahure.