Creates a session for a user.
Arguments
userId string required
The ID of the user
tags string[]
The tags associated with the session to be created. Leave empty if using defaults. See the Session Tags documentation for more information
userAgent string
Information about the user agent, such as their OS. See the docs here for more information.
ipAddress string
The user's IP Address. Including this value will include the user's IP address in the session audit logs. It is also required if you have IP Address restrictions configured. See the docs here for more information.
metadata JsonValue
Additional information to store about the user session.
deviceRegistration DeviceRegistration
Link the session to a specific device. See the Device Registration docs for more information.
Successful Response
sessionId string
The ID of the created session
sessionToken string
The token for the created session
expiresAt number
The Unix timestamp when the session expires
newDeviceDetected boolean
Whether a new device was detected (only present when using device registration)
Error Types
SessionLimitExceeded
The user has exceeded the maximum number of allowed active sessions
IpAddressError
The provided IP address is either on your blocklist, not allowed, or not specified when it should be
TagParseError
The tags provided could not be parsed
InvalidDeviceRegistration
The device registration data provided was invalid
UnexpectedError
An unexpected error occurred during the operation
const auth = createClient({ url, integrationKey });
const result = await auth.session.create({ userId: "1189c444-8a2d-4c41-8b4b-ae43ce79a492", tags: [ "type:high_security" ], userAgent: "Mozilla/5.0 (Macintosh...", ipAddress: "136.38.37.199", metadata: { "example": "value" }, deviceRegistration: { signedDeviceChallenge: "eyJhbGciOiJF..", rememberDevice: true, requestMethod: "POST", requestUrl: "https://api.example.com/api/user", },});
if (result.ok) { console.log("Session created successfully"); // Use result.data.sessionToken for authentication res.cookie("session", result.data.sessionToken);} else { console.log(`Error: ${result.error}`); // Check result.error.type to handle specific errors}client = create_client(url=url, integration_key=integration_key)
result = await client.session.create( user_id="1189c444-8a2d-4c41-8b4b-ae43ce79a492", tags=[ "type:high_security" ], user_agent="Mozilla/5.0 (Macintosh...", ip_address="136.38.37.199", metadata={ "example": "value" }, device_registration={ "signed_device_challenge": "eyJhbGciOiJF..", "remember_device": True, "request_method": "POST", "request_url": "https://api.example.com/api/user", },)
if is_ok(result): print("Session created successfully") # Use result.data.session_token for authentication response.set_cookie("session", result.data.session_token)else: print(f"Error: {result.error}") # Check result.error.type to handle specific errorsclient, err := byo.NewClient(url, integrationKey)if err != nil { log.Fatal(err)}
result, err := client.Session.Create(ctx, byo.CreateSessionCommand{ UserID: "1189c444-8a2d-4c41-8b4b-ae43ce79a492", Tags: []string{"type:high_security"}, UserAgent: byo.String("Mozilla/5.0 (Macintosh..."), IPAddress: byo.String("136.38.37.199"), Metadata: map[string]any{ "example": "value", }, DeviceRegistration: &byo.DeviceRegistration{ SignedDeviceChallenge: "eyJhbGciOiJF..", RememberDevice: byo.Bool(true), RequestMethod: byo.String("POST"), RequestURL: byo.String("https://api.example.com/api/user"), },})if err != nil { log.Fatal(err)}
fmt.Println(result.SessionToken)PropelAuthClient client = PropelAuthClient.create(url, integrationKey);
CreateSessionCommand command = CreateSessionCommand.builder() .userId("1189c444-8a2d-4c41-8b4b-ae43ce79a492") .tags(Arrays.asList("type:high_security")) .userAgent("Mozilla/5.0 (Macintosh...") .ipAddress("136.38.37.199") .metadata(JsonValue.of(Map.of("example", "value"))) .deviceRegistration(DeviceRegistration.builder() .signedDeviceChallenge("eyJhbGciOiJF..") .rememberDevice(true) .requestMethod("POST") .requestUrl("https://api.example.com/api/user") .build()) .build();
try { CreateSessionResponse sessionResponse = client.session.create(command); System.out.println("Session created successfully"); // Use sessionResponse.getSessionToken() for authentication response.addCookie(new Cookie("session", sessionResponse.getSessionToken()));} catch (CreateSessionException.SessionLimitExceeded e) { System.out.println("Session limit exceeded: " + e.getDetails().getMaxAllowed());} catch (CreateSessionException e) { System.out.println("Error: " + e.getMessage());}var client = new PropelAuthClient(new PropelAuthOptions { Url = url, IntegrationKey = integrationKey });
var command = new CreateSessionCommand{ UserId = "1189c444-8a2d-4c41-8b4b-ae43ce79a492", Tags = new List<string> { "type:high_security" }, UserAgent = "Mozilla/5.0 (Macintosh...", IpAddress = "136.38.37.199", Metadata = JsonSerializer.SerializeToElement(new Dictionary<string, object> { { "example", "value" } }), DeviceRegistration = new DeviceRegistration { SignedDeviceChallenge = "eyJhbGciOiJF..", RememberDevice = true, RequestMethod = "POST", RequestUrl = "https://api.example.com/api/user" }};
try{ var sessionResponse = await client.Session.CreateAsync(command); Console.WriteLine("Session created successfully"); // Use sessionResponse.SessionToken for authentication response.Cookies.Append("session", sessionResponse.SessionToken);}catch (CreateSessionException.SessionLimitExceeded ex){ Console.WriteLine($"Session limit exceeded: {ex.Details.MaxAllowed}");}catch (CreateSessionException ex){ Console.WriteLine($"Error: {ex.Message}");}
Response
{ ok: true, data: { sessionId: "vNCf0b...", sessionToken: "sess_vNCf0boJ...", expiresAt: 1735689600, newDeviceDetected: true }}Result( data=CreateSessionResponse( session_id="vNCf0b...", session_token="sess_vNCf0boJ...", expires_at=1735689600, new_device_detected=True ))byo.CreateSessionResponse{ SessionID: "vNCf0b...", SessionToken: "sess_vNCf0boJ...", ExpiresAt: 1735689600, NewDeviceDetected: byo.Bool(true),}CreateSessionResponse( sessionId="vNCf0b...", sessionToken="sess_vNCf0boJ...", expiresAt=1735689600, newDeviceDetected=true)CreateSessionResponse{ SessionId = "vNCf0b...", SessionToken = "sess_vNCf0boJ...", ExpiresAt = 1735689600, NewDeviceDetected = true}