# Log in and get an access token

Authenticates against the Apteco Orbit API and outputs the access token.

Adam Robertson · Updated 20 November 2026 · [Authentication](<https://help.apteco.com/en/admin/orbit-api/?category=Authentication>)

## How it works

1. Calls POST `/{dataViewName}/Sessions/SimpleLogin` to obtain an access token.
2. If the user has multi-factor authentication (MFA) turned on, SimpleLogin returns an MFA challenge instead. The script asks for the 6-digit code from the user's authenticator app and calls POST `/{dataViewName}/Sessions/Mfa/Totp/Verify` to obtain the access token.

MFA codes change every 30 seconds and must be typed in by a person, so users with MFA turned on can't run this script unattended.

## Before you start

You need:

- The base URL of the Orbit API (see [Find your API URLs](<https://help.apteco.com/en/admin/orbit-api/getting-started/#find-your-api-urls>))
- An Orbit username and password for the DataView

To keep your password and access token safe, see [Keep your credentials safe](<https://help.apteco.com/en/admin/orbit-api/getting-started/#keep-your-credentials-safe>).

## Parameters

| Parameter | Description |
| --- | --- |
| `-BaseUrl` | The base URL of the Orbit API, for example `https://example.com/OrbitAPI` |
| `-DataViewName` | The name of the DataView to act on, for example `holidays` |
| `-Username` | The username to authenticate with, for example `myuser` |
| `-Password` Optional | The password to authenticate with. If omitted, PowerShell will prompt securely. |
| `-MfaCode` Optional | The current 6-digit code from the user's authenticator app. Only used if the user has MFA turned on. If omitted, PowerShell will prompt for it when needed. |

## Example

PowerShell

```powershell
.\Login.ps1 `
    -BaseUrl "https://example.com/OrbitAPI" `
    -DataViewName "holidays" `
    -Username "myuser"
```

## Script

The script from the repository at commit `b732915`. Its built-in help is left out here, because this page shows it above. The download and the copy on GitHub include it.

> [!WARNING]
>
> Apteco provides these scripts as examples only. They're not officially supported. Review each script and test it on a non-production system before you run it against live data.

PowerShell

```powershell
[CmdletBinding()]
param (
    [Parameter(Mandatory = $true)]
    [string]$BaseUrl,

    [Parameter(Mandatory = $true)]
    [string]$DataViewName,

    [Parameter(Mandatory = $true)]
    [string]$Username,

    [Parameter(Mandatory = $false)]
    [string]$Password,

    [Parameter(Mandatory = $false)]
    [string]$MfaCode
 )

# ---------------------------------------------------------------------------
# Helper: exit with a clear error message
# ---------------------------------------------------------------------------
function Stop-WithError {
    param([string]$Message)
    Write-Error $Message
    exit 1
}

# ---------------------------------------------------------------------------
# Authenticate via SimpleLogin
# ---------------------------------------------------------------------------
$loginUrl = "$BaseUrl/$DataViewName/Sessions/SimpleLogin"

if ([string]::IsNullOrEmpty($Password)) {
    $securePassword = Read-Host -AsSecureString "Enter password"
    $bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword)
    $Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
    [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
}

$loginBody = "UserLogin=$([Uri]::EscapeDataString($Username))&Password=$([Uri]::EscapeDataString($Password))"

Write-Host "Authenticating as '$Username' against '$loginUrl'..." -ForegroundColor Cyan

try {
    $loginResponse = Invoke-RestMethod `
        -Uri         $loginUrl `
        -Method      POST `
        -ContentType 'application/x-www-form-urlencoded' `
        -Body        $loginBody `
        -ErrorAction Stop
}
catch {
    $statusCode = if ($_.Exception.Response) { $_.Exception.Response.StatusCode.value__ } else { $null }
    $errorBody  = if ($_.ErrorDetails) { $_.ErrorDetails.Message } else { $null }
    Stop-WithError "Login failed (HTTP $statusCode). Response: $errorBody`nError: $_"
}

# ---------------------------------------------------------------------------
# Complete the MFA challenge, if the user has MFA turned on
## ---------------------------------------------------------------------------
if ($loginResponse.state -eq 'SetupRequired') {
    Stop-WithError "User '$Username' must set up MFA before logging in. Log in to Orbit in a web browser to set it up, then run this script again."
}

if ($loginResponse.state -eq 'Challenge') {
    if ([string]::IsNullOrEmpty($MfaCode)) {
        $MfaCode = Read-Host "Enter the 6-digit code from your authenticator app"
    }

    $verifyUrl  = "$BaseUrl/$DataViewName/Sessions/Mfa/Totp/Verify"
    $verifyBody = @{
        mfaAccessToken = $loginResponse.mfaAccessToken
        code           = $MfaCode
    } | ConvertTo-Json

    Write-Host "Verifying MFA code against '$verifyUrl'..." -ForegroundColor Cyan

    try {
        $loginResponse = Invoke-RestMethod `
            -Uri         $verifyUrl `
            -Method      POST `
            -ContentType 'application/json' `
            -Body        $verifyBody `
            -ErrorAction Stop
    }
    catch {
        $statusCode = if ($_.Exception.Response) { $_.Exception.Response.StatusCode.value__ } else { $null }
        $errorBody  = if ($_.ErrorDetails) { $_.ErrorDetails.Message } else { $null }
        Stop-WithError "MFA verification failed (HTTP $statusCode). The code may be wrong or out of date. Response: $errorBody`nError: $_"
    }
}

# The session details include an accessToken property
$accessToken = $loginResponse.AccessToken

if ([string]::IsNullOrWhiteSpace($accessToken)) {
    Stop-WithError "Login succeeded but no AccessToken was returned. Full response: $($loginResponse | ConvertTo-Json -Depth 5)"
}

Write-Output $accessToken
Write-Host "Login successful. Access token obtained." -ForegroundColor Green
```
