> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://developer.crehana.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://developer.crehana.com/_mcp/server.

# Authorization endpoint (Authorization Code + PKCE)

GET https://www.crehana.com/api/v5/rest/auth/oauth2/authorize

Starts the Authorization Code flow with PKCE. Requires a valid session. On success responds with a 302 redirect to the client's redirect_uri carrying code and state. When there is no valid session (login_required) it redirects (302) to the login page with the original authorize URL in next_url. Other typed OAuth errors do NOT redirect and return an OAuth error body.

Reference: https://developer.crehana.com/api-reference/endpoints/o-auth-2/authorize

## Authentication

- `Api-Key` header (required) — API key of the organization. Always sent together with the `Secret-Access` header.

## Servers

- `https://www.crehana.com` (Production, default)
- `https://qa.creha.co` (QA)

## Request

### Query parameters

- `response_type` (enum, required) — Must be 'code'
  - Allowed values: `code`
- `client_id` (string, required) — OAuth client identifier
- `redirect_uri` (string, required) — Client redirect URI
- `scope` (string, optional) — Space-separated scopes
- `state` (string, optional) — Opaque client state
- `code_challenge` (string, optional) — PKCE code challenge
- `code_challenge_method` (enum, optional) — PKCE method (S256)
  - Allowed values: `S256`
- `nonce` (string, optional) — OIDC nonce

## Response

### 200

Successful Response

- `any`

## Errors

### 400 Bad Request Error

Bad Request

- `error` (string, required) — OAuth error code (RFC 6749 §5.2)
- `error_description` (string, optional) — Human-readable error description
- `state` (string, optional) — State echoed back on the /authorize error path

### 401 Unauthorized Error

Unauthorized

- `error` (string, required) — OAuth error code (RFC 6749 §5.2)
- `error_description` (string, optional) — Human-readable error description
- `state` (string, optional) — State echoed back on the /authorize error path

### 422 Unprocessable Entity Error

Validation Error

- `detail` (list of ValidationError, optional)

## Types

### ValidationError

- `loc` (list of ValidationErrorLocItems, required)
- `msg` (string, required)
- `type` (string, required)

### ValidationErrorLocItems

- string
- integer

## Examples

**SDK Code**

```python
import requests

url = "https://www.crehana.com/api/v5/rest/auth/oauth2/authorize"

querystring = {"client_id":"client_id","redirect_uri":"redirect_uri","response_type":"code"}

headers = {
    "Secret-Access": "Secret-Access",
    "Api-Key": "<apiKey>"
}

response = requests.get(url, headers=headers, params=querystring)

print(response.json())
```

```javascript
const url = 'https://www.crehana.com/api/v5/rest/auth/oauth2/authorize?client_id=client_id&redirect_uri=redirect_uri&response_type=code';
const options = {
  method: 'GET',
  headers: {'Secret-Access': 'Secret-Access', 'Api-Key': '<apiKey>'}
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go
package main

import (
	"fmt"
	"net/http"
	"io"
)

func main() {

	url := "https://www.crehana.com/api/v5/rest/auth/oauth2/authorize?client_id=client_id&redirect_uri=redirect_uri&response_type=code"

	req, _ := http.NewRequest("GET", url, nil)

	req.Header.Add("Secret-Access", "Secret-Access")
	req.Header.Add("Api-Key", "<apiKey>")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby
require 'uri'
require 'net/http'

url = URI("https://www.crehana.com/api/v5/rest/auth/oauth2/authorize?client_id=client_id&redirect_uri=redirect_uri&response_type=code")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Get.new(url)
request["Secret-Access"] = 'Secret-Access'
request["Api-Key"] = '<apiKey>'

response = http.request(request)
puts response.read_body
```

```java
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.get("https://www.crehana.com/api/v5/rest/auth/oauth2/authorize?client_id=client_id&redirect_uri=redirect_uri&response_type=code")
  .header("Secret-Access", "Secret-Access")
  .header("Api-Key", "<apiKey>")
  .asString();
```

```php
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('GET', 'https://www.crehana.com/api/v5/rest/auth/oauth2/authorize?client_id=client_id&redirect_uri=redirect_uri&response_type=code', [
  'headers' => [
    'Api-Key' => '<apiKey>',
    'Secret-Access' => 'Secret-Access',
  ],
]);

echo $response->getBody();
```

```csharp
using RestSharp;

var client = new RestClient("https://www.crehana.com/api/v5/rest/auth/oauth2/authorize?client_id=client_id&redirect_uri=redirect_uri&response_type=code");
var request = new RestRequest(Method.GET);
request.AddHeader("Secret-Access", "Secret-Access");
request.AddHeader("Api-Key", "<apiKey>");
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = [
  "Secret-Access": "Secret-Access",
  "Api-Key": "<apiKey>"
]

let request = NSMutableURLRequest(url: NSURL(string: "https://www.crehana.com/api/v5/rest/auth/oauth2/authorize?client_id=client_id&redirect_uri=redirect_uri&response_type=code")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```