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

# Authenticate user and get access token

POST /v1/login
Content-Type: application/json

Authenticates a user with username/password and returns a token
for use in subsequent API calls. Also sets a session cookie.

The returned access token should be included in the Authorization header
for subsequent requests in the format: `Authorization: Bearer {token}`.

Reference: https://ocient.ferndocs.com/api-reference/authentication/login

## Request

### Body (application/json)

This endpoint expects a LoginRequest.

- `username` (string, required) — User's login name
- `password` (string, required) — User's password
- `database` (string, optional) — Target database name (defaults to system if not specified)

## Response

### 200

Successfully authenticated

- `access_token` (string, required) — Access token for subsequent API calls. This token should be included in the Authorization header for all authenticated requests using the format: `Authorization: Bearer {token}`.
- `status` (Status, required) — Status information about the login operation. A successful login will have a SQL state of "00000".
- `username` (string, optional) — The authenticated user's username. This is useful for displaying in UIs or for audit logging purposes.
- `database` (string, optional) — The database that was authenticated against. This is the default database for subsequent queries if none is specified.
- `expires_in` (integer, optional) — Number of seconds until the access token expires. Clients should refresh the token before this time to maintain uninterrupted access.

## Errors

### 401 Unauthorized Error

Unauthorized

- `status` (Status, required)

### 404 Not Found Error

Object not found

- `status` (Status, required)

### 500 Internal Server Error

Server Error

- `status` (Status, required)

## Types

### Status

- `reason` (string, required) — Human-readable description of the operation result
- `sql_state` (string, required) — SQL state code (see Error Handling section for common codes)
- `vendor_code` (integer, required) — Vendor-specific error code

## Examples

### Successful login response

**Response**

```json
{
  "access_token": "eyJhb...",
  "status": {
    "reason": "Authentication successful",
    "sql_state": "00000",
    "vendor_code": 0
  },
  "username": "jdoe",
  "database": "retail_analytics"
}
```

**SDK Code**

```python Successful login response
import requests

url = "https://v1/login"

response = requests.post(url)

print(response.json())
```

```javascript Successful login response
const url = 'https://v1/login';
const options = {method: 'POST'};

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

```go Successful login response
package main

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

func main() {

	url := "https://v1/login"

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

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

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

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

}
```

```ruby Successful login response
require 'uri'
require 'net/http'

url = URI("https://v1/login")

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

request = Net::HTTP::Post.new(url)

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

```java Successful login response
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://v1/login")
  .asString();
```

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

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://v1/login');

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

```csharp Successful login response
using RestSharp;

var client = new RestClient("https://v1/login");
var request = new RestRequest(Method.POST);
IRestResponse response = client.Execute(request);
```

```swift Successful login response
import Foundation

let request = NSMutableURLRequest(url: NSURL(string: "https://v1/login")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"

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()
```

### Basic Login Request

**Request**

```json
{
  "username": "jdoe",
  "password": "secure_password",
  "database": "retail_analytics"
}
```

**Response**

```json
{
  "access_token": "eyJhb...",
  "status": {
    "reason": "Authentication successful",
    "sql_state": "00000",
    "vendor_code": 0
  },
  "username": "jdoe",
  "database": "retail_analytics"
}
```

**SDK Code**

```python Basic Login Request
import requests

url = "https://v1/login"

payload = {
    "username": "jdoe",
    "password": "secure_password",
    "database": "retail_analytics"
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript Basic Login Request
const url = 'https://v1/login';
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: '{"username":"jdoe","password":"secure_password","database":"retail_analytics"}'
};

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

```go Basic Login Request
package main

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

func main() {

	url := "https://v1/login"

	payload := strings.NewReader("{\n  \"username\": \"jdoe\",\n  \"password\": \"secure_password\",\n  \"database\": \"retail_analytics\"\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/json")

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

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

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

}
```

```ruby Basic Login Request
require 'uri'
require 'net/http'

url = URI("https://v1/login")

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

request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n  \"username\": \"jdoe\",\n  \"password\": \"secure_password\",\n  \"database\": \"retail_analytics\"\n}"

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

```java Basic Login Request
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://v1/login")
  .header("Content-Type", "application/json")
  .body("{\n  \"username\": \"jdoe\",\n  \"password\": \"secure_password\",\n  \"database\": \"retail_analytics\"\n}")
  .asString();
```

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

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://v1/login', [
  'body' => '{
  "username": "jdoe",
  "password": "secure_password",
  "database": "retail_analytics"
}',
  'headers' => [
    'Content-Type' => 'application/json',
  ],
]);

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

```csharp Basic Login Request
using RestSharp;

var client = new RestClient("https://v1/login");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"username\": \"jdoe\",\n  \"password\": \"secure_password\",\n  \"database\": \"retail_analytics\"\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift Basic Login Request
import Foundation

let headers = ["Content-Type": "application/json"]
let parameters = [
  "username": "jdoe",
  "password": "secure_password",
  "database": "retail_analytics"
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://v1/login")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

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()
```