Skip to main content

Profile API

The Profile API allows authenticated users to view and update their profile information. Profiles are automatically created on first access.

Authentication

All endpoints require:
  • Valid user session
  • Session cookie or Bearer token

Get user profile

Retrieve the authenticated user’s profile. If no profile exists, one is created automatically. Endpoint: GET /api/profile
Response:
string
Profile UUID
string
User ID (references auth user table)
string
User’s full name
text
User biography/description
string
Profile picture URL
string
User location (city, region)
string
User website URL
string
Preferred language code: "fr", "en", "ar", or "es"
timestamp
Profile creation timestamp
timestamp
Last update timestamp
Example response:

Update user profile

Update the authenticated user’s profile. Only specified fields are updated. Endpoint: PATCH /api/profile
Request Body: All fields are optional. Only provided fields will be updated.
string
User’s full name
text
Biography or description
string
Profile picture URL
string
Location (city, region, country)
string
Personal website URL
string
Language preference: "fr", "en", "ar", or "es"
Security: Only the authenticated user can update their own profile. The userId field cannot be changed and is enforced by the API.
Response: Returns the updated profile object with the same structure as GET.

Auto-creation behavior

When a user first accesses their profile via GET /api/profile:
  1. Profile exists: Returns existing profile
  2. No profile: Automatically creates one with:
    • fullName populated from auth user’s name
    • preferredLanguage set to "fr" (default)
    • Other fields set to null
This ensures every authenticated user has a profile without requiring explicit creation.

Validation rules

The API validates update requests:
  • Allowed fields only: Only the 6 editable fields can be updated
  • No userId changes: The userId field is immutable
  • Type checking: Fields must match expected types
  • SQL injection prevention: All inputs are parameterized
Attempting to update disallowed fields will silently ignore them.

Response codes

success
Profile retrieved or updated successfully
error
Invalid request body (malformed JSON)
error
Unauthorized - valid session required
error
Database error or internal server error

Error responses

Implementation reference

Source: /src/pages/api/profile/index.ts The profile endpoint:
  • Uses Better Auth session validation
  • Queries the profile table with Drizzle ORM
  • Auto-creates profiles on first GET request
  • Validates updates against a whitelist of allowed fields
  • Sets security headers (X-Content-Type-Options: nosniff)

Database schema

The profile table structure:
The preferredLanguage field integrates with the i18n system. When set, the application UI will display in the user’s preferred language across all routes.

See also