> ## Documentation Index
> Fetch the complete documentation index at: https://docs.auth-agent.com/llms.txt
> Use this file to discover all available pages before exploring further.

# GET /authorize

> OAuth 2.1 authorization endpoint

## Overview

The authorization endpoint is used to initiate the OAuth 2.1 authorization flow. This endpoint presents the user with an authorization page where they can approve the agent's access request.

```
GET https://api.auth-agent.com/authorize
```

## Query Parameters

**client\_id** `string` *required*

The OAuth client identifier provided during client registration

**redirect\_uri** `string` *required*

The URI where the user will be redirected after authorization. Must exactly match one of the registered redirect URIs for the client. Must use HTTPS except for localhost.

**response\_type** `string` *required*

Must be `code` for authorization code flow

**state** `string` *required*

An opaque value used to maintain state between the request and callback. This prevents CSRF attacks. Should be a random, unguessable string.

**code\_challenge** `string` *required*

PKCE code challenge derived from the code verifier using SHA-256

**code\_challenge\_method** `string` *required*

Must be `S256` (SHA-256 hashing). Plain code challenges are not supported.

**scope** `string`

Space-separated list of scopes requested. If not provided, defaults to `openid email profile`.

Available scopes:

* `openid` - OpenID Connect authentication
* `email` - Access to user email address
* `profile` - Access to user profile (name)

## Response

### Success (302 Redirect)

If validation succeeds, the user is presented with an authorization page. After the agent authenticates via back-channel and the user approves, they are redirected to the `redirect_uri` with the authorization code:

```
{redirect_uri}?code={authorization_code}&state={state}
```

#### Redirect Parameters

| Parameter | Type | Description |
| - | - | - |
| `code` | string | Authorization code to exchange for tokens (expires in 10 minutes) |
| `state` | string | The same state value provided in the authorization request |

### Error (400 Bad Request)

If validation fails, an HTML error page is displayed with the error details:

```json theme={null}
{
  "error": "invalid_request",
  "error_description": "Missing client_id parameter"
}
```

## Error Codes

| Error Code | Description |
| - | - |
| `invalid_request` | Missing or invalid required parameter |
| `invalid_client` | Client not found or inactive |
| `unsupported_response_type` | Response type is not `code` |

## Flow Diagram

```mermaid theme={null}
sequenceDiagram
    participant User
    participant Website
    participant AuthAgent
    participant Agent

    Website->>AuthAgent: GET /authorize with PKCE params
    AuthAgent->>User: Display authorization page
    Agent->>AuthAgent: POST /api/agent/authenticate (back-channel)
    AuthAgent->>Agent: Authorization code created
    AuthAgent->>User: Redirect to redirect_uri with code
    User->>Website: code + state
    Website->>AuthAgent: POST /token (exchange code)
    AuthAgent->>Website: access_token + refresh_token
```

## Example Request

```typescript Next.js theme={null}
// Generate PKCE parameters
const codeVerifier = generateCodeVerifier();
const codeChallenge = await generateCodeChallenge(codeVerifier);

// Store code verifier for later use in token exchange
sessionStorage.setItem('code_verifier', codeVerifier);

// Build authorization URL
const params = new URLSearchParams({
  client_id: 'client_abc123',
  redirect_uri: 'https://yourwebsite.com/api/auth/callback',
  response_type: 'code',
  code_challenge: codeChallenge,
  code_challenge_method: 'S256',
  scope: 'openid email profile',
  state: generateRandomState(),
});

// Redirect user to Auth Agent
window.location.href = `https://api.auth-agent.com/authorize?${params}`;
```

```typescript Express theme={null}
app.get('/auth/login', async (req, res) => {
  const codeVerifier = generateCodeVerifier();
  const codeChallenge = await generateCodeChallenge(codeVerifier);

  // Store code verifier in session
  req.session.codeVerifier = codeVerifier;

  const params = new URLSearchParams({
    client_id: process.env.AUTH_AGENT_CLIENT_ID,
    redirect_uri: `${process.env.BASE_URL}/api/auth/callback`,
    response_type: 'code',
    code_challenge: codeChallenge,
    code_challenge_method: 'S256',
    scope: 'openid email profile',
    state: generateRandomState(),
  });

  res.redirect(`https://api.auth-agent.com/authorize?${params}`);
});
```

```python Flask theme={null}
from urllib.parse import urlencode
from flask import redirect, session
from auth_agent import generate_pkce

@app.route('/auth/login')
def login():
    # Generate PKCE parameters
    code_verifier, code_challenge = generate_pkce()

    # Store code verifier in session
    session['code_verifier'] = code_verifier

    # Build authorization URL
    params = {
        'client_id': os.environ['AUTH_AGENT_CLIENT_ID'],
        'redirect_uri': f"{os.environ['BASE_URL']}/api/auth/callback",
        'response_type': 'code',
        'code_challenge': code_challenge,
        'code_challenge_method': 'S256',
        'scope': 'openid email profile',
        'state': generate_random_state(),
    }

    auth_url = f"https://api.auth-agent.com/authorize?{urlencode(params)}"
    return redirect(auth_url)
```

## PKCE Implementation

> [accordion] Generating Code Verifier and Challenge
>
> ```typescript theme={null}
> // Generate code verifier (random string)
> export function generateCodeVerifier(): string {
>   const array = new Uint8Array(32);
>   crypto.getRandomValues(array);
>   return base64URLEncode(array);
> }
>
> // Generate code challenge (SHA-256 hash of verifier)
> export async function generateCodeChallenge(verifier: string): Promise<string> {
>   const encoder = new TextEncoder();
>   const data = encoder.encode(verifier);
>   const hash = await crypto.subtle.digest('SHA-256', data);
>   return base64URLEncode(new Uint8Array(hash));
> }
>
> function base64URLEncode(buffer: Uint8Array): string {
>   const base64 = btoa(String.fromCharCode(...buffer));
>   return base64
>     .replace(/\+/g, '-')
>     .replace(/\//g, '_')
>     .replace(/=/g, '');
> }
> ```

## Security Considerations

> [!WARNING]
>
> **Always use HTTPS** for redirect URIs in production. HTTP is only allowed for localhost during development.

> [!NOTE]
>
> **State parameter** is required to prevent CSRF attacks. Generate a random, unguessable value and verify it in your callback handler.

> [!NOTE]
>
> **Code verifier** must be stored securely (session, cookie, or memory) and used in the subsequent token exchange request.

## Next Steps

After successfully receiving an authorization code:

> [card] [Exchange Code for Tokens](https://docs.auth-agent.com/api-reference/oauth/token.md)
>
> POST /token - Exchange the authorization code for access tokens

> [card] [Get User Info](https://docs.auth-agent.com/api-reference/oauth/userinfo.md)
>
> GET /userinfo - Retrieve user information using the access token


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.