Skip to content

Authentication

TerraScale supports two authentication methods for different use cases.


Used for programmatic access to database operations. API keys are generated from the dashboard and provide scoped access to your resources.

Authorization: Bearer ts_live_your_api_key
ScopeDescription
database:readRead items from databases
database:writeWrite/delete items in databases
database:*Full database access
repository:readRead entities from repositories
repository:writeWrite/delete entities in repositories
repository:*Full repository access
*Full access to all operations
var client = new TerraScaleDatabase(new TerraScaleDatabaseOptions
{
ApiKey = "ts_live_your_api_key",
Endpoint = "https://api.terrascale.io",
DefaultDatabase = "my-database"
});
  • Use specific scopes: Only grant the permissions your application needs
  • Set expiration dates: Rotate keys regularly for security
  • Separate keys per environment: Use different keys for development and production
  • Never commit keys to source control: Use environment variables or secret managers

Used for dashboard access and user sessions. JWT tokens are obtained through the login flow.

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
TokenLifetimeUsage
Access Token1 hourAuthenticate API requests
Refresh Token7 daysObtain new access tokens

Logs in with email and password.

Request:

{
"email": "john@example.com",
"password": "SecurePassword123!"
}

Response (Success):

{
"status": "Success",
"authResponse": {
"accessToken": "eyJhbGci...",
"refreshToken": "rt_abc123...",
"expiresIn": 3600,
"userId": "usr_abc123",
"email": "john@example.com",
"name": "John Doe",
"avatarUrl": null,
"personalOrgId": "org_abc123",
"permissions": ["database:read", "database:write"],
"roles": ["member"]
}
}
StatusDescription
SuccessLogin successful, tokens returned
InvalidCredentialsWrong email or password
EmailVerificationRequiredMust verify email first
MfaRequiredMust complete MFA challenge
ErrorAn error occurred

When your access token expires, use the refresh token to obtain a new one:

Request:

{
"refreshToken": "rt_abc123..."
}

Response:

{
"accessToken": "eyJhbGci...",
"refreshToken": "rt_def456...",
"expiresIn": 3600
}
// The Management Client handles token refresh automatically
var client = new ManagementClient(new ManagementClientOptions
{
ApiBaseUrl = "https://api.terrascale.io"
});
// After login
client.SetAccessToken(authResponse.AccessToken);
// When token expires, refresh it
var refreshResult = await client.Auth.RefreshTokenAsync(
new RefreshTokenRequest(RefreshToken: currentRefreshToken)
);
if (refreshResult.IsSuccess)
{
client.SetAccessToken(refreshResult.Value.AccessToken);
}

The authentication response includes user information and permissions:

FieldTypeDescription
accessTokenstringJWT for authenticating requests
refreshTokenstringToken for obtaining new access tokens
expiresInintegerAccess token lifetime in seconds
userIdstringUnique user identifier
emailstringUser’s email address
namestringUser’s display name
avatarUrlstringURL to user’s avatar image
personalOrgIdstringUser’s personal organization ID
permissionsarrayList of granted permissions
rolesarrayList of assigned roles