Zum Hauptinhalt springen

Update User Endpoint

Overview

This endpoint updates a user's email and/or password and refreshes their session.

Request Details

HTTP Method

PATCH

Route

/api/users/[user_id]

Route Parameters

ParameterTypeRequiredDescription
user_idstringYesUnique identifier of the user

Headers

HeaderValueRequiredDescription
Content-Typeapplication/jsonYesIndicates JSON request body
Cookieneptun-sessionYesSession authentication cookie

Query Parameters

No query parameters required.

Request Body

At least one of the following fields must be provided:

FieldTypeRequiredDescriptionConstraints
emailstringNoNew email address for the userValid email format
passwordstringNoNew password for the userNon-empty string

Response Format

Success Response (200 OK)

{
"user": {
"id": "user123",
"primary_email": "newemail@example.com"
}
}

Error Responses

Bad Request (400)

{
"statusCode": 400,
"statusMessage": "Bad Request.",
"message": "Invalid body(?email, ?password). At least one of email or password is required.",
"data": {
"issues": [
{
"code": "invalid_string",
"validation": "email",
"message": "Invalid email"
}
]
}
}

TypeScript Interface

interface UpdateUserRequest {
email?: string
password?: string
}

interface UpdateUserResponse {
user: {
id: string
primary_email: string
}
}

interface UpdateUserError {
statusCode: number
statusMessage: string
message: string
data?: {
issues: Array<{
code: string
validation: string
message: string
}>
}
}

Python Model

from pydantic import BaseModel, EmailStr
from typing import Optional

class UpdateUserRequest(BaseModel):
email: Optional[EmailStr] = None
password: Optional[str] = None

class UserInfo(BaseModel):
id: str
primary_email: EmailStr

class UpdateUserResponse(BaseModel):
user: UserInfo

Code Examples

Python Example (using httpx)

from pydantic import BaseModel, EmailStr
import httpx
from typing import Optional

class UpdateUserRequest(BaseModel):
email: Optional[EmailStr] = None
password: Optional[str] = None

class UserInfo(BaseModel):
id: str
primary_email: EmailStr

class UpdateUserResponse(BaseModel):
user: UserInfo

async def update_user(
user_id: str,
session_cookie: str,
email: Optional[str] = None,
password: Optional[str] = None
) -> UpdateUserResponse:
update_data = {}
if email:
update_data["email"] = email
if password:
update_data["password"] = password

async with httpx.AsyncClient() as client:
response = await client.patch(
f"https://neptun-webui.vercel.app/api/users/{user_id}",
json=update_data,
cookies={"neptun-session": session_cookie}
)
response.raise_for_status()
return UpdateUserResponse(**response.json())

cURL Example

curl -X PATCH \
-H "Content-Type: application/json" \
-H "Cookie: neptun-session=your-session-cookie" \
-d '{
"email": "newemail@example.com",
"password": "newpassword"
}' \
"https://neptun-webui.vercel.app/api/users/your-user-id"

TypeScript/JavaScript Example (using fetch)

async function updateUser(
userId: string,
updates: {
email?: string
password?: string
}
): Promise<UpdateUserResponse> {
const response = await fetch(
`https://neptun-webui.vercel.app/api/users/${userId}`,
{
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(updates),
credentials: 'include', // Important for cookie handling
}
)

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}

return await response.json() as UpdateUserResponse
}

Response Status Codes

Status CodeDescription
200User successfully updated
400Invalid request body or missing required fields
401Unauthorized (invalid or missing session)
404User not found
500Server error during update

Notes

  • At least one of email or password must be provided in the request body
  • The session is automatically updated if the email is changed
  • The session cookie is required for authentication
  • All fields in the request body are optional, but at least one must be present