# API key बनाएँ

कैनोनिकल URL: https://sosoapi.ai/hi/docs/api/identity-api-api-keys-create

कॉन्ट्रैक्ट: sha256:563d2d581da0ad29

चुनी गई क्षमताओं (agent:operate, agent:publish, agent:engage), वैकल्पिक workspace_ids सीमा और अधिकतम 365 दिन की अवधि वाली निजी एक्सेस key बनाएँ; सीक्रेट केवल एक बार लौटाया जाता है और दोबारा नहीं पढ़ा जा सकता। एजेंट केवल अपने प्राधिकरण में उपलब्ध अधिकार, अधिकतम 30 दिनों के लिए दे सकता है, और प्राधिकरण समाप्त होने पर key काम करना बंद कर देती है। Manage API keys का अधिकार आवश्यक है; PAT इसका उपयोग नहीं कर सकता।

हर प्रत्यायोजित प्राधिकरण में अधिकतम 25 सक्रिय keys हो सकती हैं। सीमा से अधिक अनुरोध API_KEY_GRANT_KEY_LIMIT लौटाते हैं।

POST /identity-api/api-keys/create

## प्रमाणीकरण और अनुमतियाँ

- **स्वीकार्य क्रेडेंशियल श्रेणियाँ:** user_session, delegated_oauth

- **प्रत्यायोजित क्षमता:** agent:manage_api_keys

- **न्यूनतम वर्कस्पेस भूमिका:** नहीं workspace-role minimum in this contract; ownership and grant checks still apply.

- **एक्सेस का दायरा:** खाता-स्तरीय अधिकार

- **सभी वर्कस्पेस के लिए सहमति आवश्यक:** हाँ

- **स्थिति बदलता है:** हाँ

- **ऑपरेशन पूरा होने पर:** synchronous_http_response

टोकन केवल मालिक के अधिकार इस्तेमाल कर सकता है। हर अनुरोध वर्कस्पेस सदस्यता, संसाधन स्वामित्व, चुने वर्कस्पेस और दी क्षमताएँ जाँचता है। Core और full प्रोफ़ाइल दिखने वाले टूल बदलते हैं; एक्सेस नहीं देते।

## अनुरोध के फ़ील्ड

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 80
    },
    "lifetime_days": {
      "default": 365,
      "description": "Days until the key expires (1-365, default 365). A key created by a delegated agent never lives longer than 30 days.",
      "type": "integer",
      "minimum": 1,
      "maximum": 365
    },
    "capabilities": {
      "description": "What the key may do. Defaults to operate, publish and engage for a signed-in user. A delegated agent can only grant what its own authorization holds (never key management).",
      "minItems": 1,
      "maxItems": 3,
      "type": "array",
      "items": {
        "type": "string",
        "enum": [
          "agent:operate",
          "agent:publish",
          "agent:engage"
        ]
      }
    },
    "workspace_ids": {
      "description": "Limit the key to these Workspaces. Omit it for every Workspace the user can access. A limited key must name its Workspace (workspace_id) on routes that would otherwise use the active Workspace, and cannot call organization-wide reads.",
      "minItems": 1,
      "maxItems": 100,
      "type": "array",
      "items": {
        "type": "string",
        "format": "uuid",
        "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
      }
    },
    "scopes": {
      "description": "Deprecated alias of capabilities. It is honoured exactly; an empty list is ignored.",
      "maxItems": 3,
      "type": "array",
      "items": {
        "type": "string",
        "enum": [
          "agent:operate",
          "agent:publish",
          "agent:engage"
        ]
      }
    },
    "idempotency_key": {
      "description": "Optional. Reuse the same key to retry safely. The secret is shown once: a retry that finds the key already created answers 409 IDEMPOTENT_REPLAY (details.existing_key names it) instead of showing it again. Revoke that key and create a new one with a new idempotency_key.",
      "type": "string",
      "minLength": 8,
      "maxLength": 160,
      "pattern": "^[!-~]+$"
    }
  },
  "required": [
    "name"
  ],
  "additionalProperties": false
}
```

## रिस्पॉन्स और पूर्णता

सफल HTTP स्टेटस: 200

सफल HTTP रिस्पॉन्स का अर्थ अनुरोध पूरा होने के बजाय स्वीकार होना हो सकता है। बैकग्राउंड काम में लौटाई ID सहेजें और समाप्ति तक वही ऑपरेशन जाँचें। निर्धारित या स्वीकृत पोस्ट प्लेटफ़ॉर्म की पुष्टि पर ही प्रकाशित है; क्लाइंट टाइमआउट परिणाम सिद्ध नहीं करता।

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "apiKey": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid",
          "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
        },
        "name": {
          "type": "string"
        },
        "last_4": {
          "type": "string"
        },
        "scopes": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "created_at": {
          "type": "string",
          "format": "date-time",
          "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d+)?)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
        },
        "expires_at": {
          "anyOf": [
            {
              "type": "string",
              "format": "date-time",
              "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d+)?)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
            },
            {
              "type": "null"
            }
          ]
        },
        "last_used_at": {
          "anyOf": [
            {
              "type": "string",
              "format": "date-time",
              "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d+)?)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
            },
            {
              "type": "null"
            }
          ]
        },
        "revoked_at": {
          "anyOf": [
            {
              "type": "string",
              "format": "date-time",
              "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d+)?)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
            },
            {
              "type": "null"
            }
          ]
        },
        "status": {
          "type": "string",
          "enum": [
            "active",
            "expired",
            "revoked"
          ]
        },
        "replaces": {
          "anyOf": [
            {
              "type": "object",
              "properties": {
                "id": {
                  "type": "string",
                  "format": "uuid",
                  "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
                },
                "name": {
                  "type": "string"
                },
                "last_4": {
                  "type": "string"
                },
                "status": {
                  "type": "string",
                  "enum": [
                    "active",
                    "expired",
                    "revoked"
                  ]
                }
              },
              "required": [
                "id",
                "name",
                "last_4",
                "status"
              ]
            },
            {
              "type": "null"
            }
          ]
        },
        "replaced_by": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "format": "uuid",
                "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
              },
              "name": {
                "type": "string"
              },
              "last_4": {
                "type": "string"
              },
              "status": {
                "type": "string",
                "enum": [
                  "active",
                  "expired",
                  "revoked"
                ]
              }
            },
            "required": [
              "id",
              "name",
              "last_4",
              "status"
            ]
          }
        },
        "capabilities": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": [
              "agent:operate",
              "agent:publish",
              "agent:engage"
            ]
          }
        },
        "workspace_ids": {
          "anyOf": [
            {
              "type": "array",
              "items": {
                "type": "string",
                "format": "uuid",
                "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
              }
            },
            {
              "type": "null"
            }
          ]
        },
        "created_by_delegated_grant_id": {
          "anyOf": [
            {
              "type": "string",
              "format": "uuid",
              "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
            },
            {
              "type": "null"
            }
          ]
        }
      },
      "required": [
        "id",
        "name",
        "last_4",
        "scopes",
        "created_at",
        "expires_at",
        "last_used_at",
        "revoked_at",
        "status",
        "replaces",
        "replaced_by"
      ]
    },
    "secret": {
      "type": "string",
      "minLength": 1
    }
  },
  "required": [
    "apiKey",
    "secret"
  ]
}
```

## त्रुटियाँ और सुरक्षित पुनः प्रयास

HTTP स्टेटस और रिस्पॉन्स बॉडी जाँचें। 400 वैलिडेशन त्रुटि में ZodError हो सकता है; हैंडलर त्रुटियाँ error, message और code फ़ील्ड इस्तेमाल करती हैं। जाँच के लिए request_id या x-request-id रखें, क्रेडेंशियल के बिना। फिर प्रयास से पहले 401/403 एक्सेस त्रुटियाँ ठीक करें और 429 के लिए Retry-After का पालन करें। राइट अनिश्चित हो या 5xx लौटाए, तो पहले मौजूदा परिणाम पढ़ें। idempotency key केवल समर्थित स्थान पर दोबारा इस्तेमाल करें; पुनः प्रयास का व्यवहार ऑपरेशन के अनुसार बदलता है और ठीक एक बार डिलीवरी की गारंटी नहीं देता।

## उदाहरण

उदाहरणों में स्कीमा से जाँचे काल्पनिक मान हैं। IDs को अधिकृत IDs, मीडिया को तैयार एसेट और तारीखों को पुष्ट प्रकाशन समय से बदलें। राइट सबमिट करने से पहले खाते की पात्रता जाँचें। यह पेज API अनुरोध नहीं भेजता।

सर्वर पर SOSOAPI_ACCESS_TOKEN को इस ऑपरेशन के स्वीकार्य क्रेडेंशियल से सेट करें और ब्राउज़र कोड से बाहर रखें। एजेंट के लिए sosoapi auth login से ब्राउज़र साइन-इन करें और CLI को क्रेडेंशियल रिफ़्रेश करने दें।

request.json

```json
{
  "name": "example"
}
```

cURL

```bash
curl --request POST \
  "https://gqeshlawtcexsapabaui.supabase.co/functions/v1/identity-api/api-keys/create" \
  -H "Authorization: Bearer $SOSOAPI_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  --data-binary @request.json
```

JavaScript

```js
// Server-side JavaScript. Do not print tokens or credential-bearing responses.
const token = process.env.SOSOAPI_ACCESS_TOKEN;
if (!token) throw new Error("Set an accepted credential for this operation");
const response = await fetch("https://gqeshlawtcexsapabaui.supabase.co/functions/v1/identity-api/api-keys/create", {
  method: "POST",
  headers: { "Content-Type": "application/json", Authorization: "Bearer " + token },
  body: JSON.stringify({
  "name": "example"
})
});
const result = await response.json();
// Check response.ok, the operation-specific body and completion state before continuing.
```

CLI

```bash
npx sosoapi api post /identity-api/api-keys/create < request.json
```

कृत्रिम रिस्पॉन्स का उदाहरण

```json
{
  "apiKey": {
    "id": "11111111-1111-4111-8111-111111111111",
    "name": "example",
    "last_4": "example",
    "scopes": [],
    "created_at": "2099-01-15T09:00:00Z",
    "expires_at": null,
    "last_used_at": null,
    "revoked_at": null,
    "status": "active",
    "replaces": null,
    "replaced_by": []
  },
  "secret": "example"
}
```

## संबंधित टूल और गाइड

MCP: sosoapi_create_api_key

[API संदर्भ](https://sosoapi.ai/hi/docs/api)
