# Complete a provider Connect flow and store the tenant's credential.

`POST /api/ai-chat/credentials/connect`

Authentication: no security requirement declared for this operation.

Request body: application/json (required)

Type: object

Schema: [ConnectAiProviderCredentialDTO](https://sourcey-gauzy.pages.dev/api-reference/models/connectaiprovidercredentialdto/index.md)

- `tenant` (optional)
  Type: object

- `tenantId` (optional)
  Type: string

- `organization` (required)
  Type: object

- `organizationId` (required)
  Type: string

- `sentTo` (optional)
  Type: string

- `providerId` (required)
  Type: string
  
  AI provider identifier (must support a connect flow)

- `code` (required)
  Type: string
  
  One-time authorization code returned by the provider

- `codeVerifier` (required)
  Type: string
  
  PKCE code verifier generated by the client before authorizing

Responses:

- 201: Connected: the exchanged API key was stored (returned masked).

- 400: Provider unknown or does not support Connect.

- 502: The provider rejected or failed the exchange.

## Examples

### cURL

```bash
curl -X POST 'http://localhost:3000/api/ai-chat/credentials/connect' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "tenant": {},
  "tenantId": "string",
  "organization": {},
  "organizationId": "string",
  "sentTo": "string",
  "providerId": "string",
  "code": "string",
  "codeVerifier": "string"
}'
```

### JavaScript

```javascript
const body = `{
  "tenant": {},
  "tenantId": "string",
  "organization": {},
  "organizationId": "string",
  "sentTo": "string",
  "providerId": "string",
  "code": "string",
  "codeVerifier": "string"
}`;

const response = await fetch('http://localhost:3000/api/ai-chat/credentials/connect', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body,
});

const data = await response.text();
```

### Python

```python
import requests

body = '''{
  "tenant": {},
  "tenantId": "string",
  "organization": {},
  "organizationId": "string",
  "sentTo": "string",
  "providerId": "string",
  "code": "string",
  "codeVerifier": "string"
}'''

response = requests.post(
    'http://localhost:3000/api/ai-chat/credentials/connect',
    data=body.encode('utf-8'),
    headers={
        'Content-Type': 'application/json',
    },
)
print(response.text)
```
