The code runtime built into the server includes a module with functions to implement various logic and custom behavior, enabling you to define authoritative code and conditions on input received by clients. Learn more about it in the Server Framework Basics documentation.
This page lists all functions available within layerG and their respective parameters, with corresponding code samples for each. If you haven’t already, see the documentation on using the server framework .
Writing custom SQL is discouraged in favor of using the built-in features of the Storage Engine.
The creation of custom tables is strongly discouraged.
Accounts
AccountDeleteId
Delete an account by user ID.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userId string REQUIRED User ID for the account to be deleted. Must be valid UUID. recorded bool REQUIRED false Whether to record this deletion in the database.
Name Description error error An optional error value if an error occurred.
if err : = lg . AccountDeleteId ( ctx , "8f4d52c7-bf28-4fcf-8af2-1d4fcf685592" , false ) ; err != nil { logger . WithField ( "err" , err ) . Error ( "Delete account error." ) }
AccountExportId
Export account information for a specified user ID.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userId string REQUIRED User ID for the account to be exported. Must be valid UUID.
Name Description export string Account information for the provided user ID, in JSON format. error error An optional error value if an error occurred.
if err : = lg . AccountExportId ( ctx , "8f4d52c7-bf28-4fcf-8af2-1d4fcf685592" ) ; err != nil { logger . WithField ( "err" , err ) . Error ( "Export account error." ) }
AccountGetId
Fetch account information by user ID.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userId string REQUIRED User ID to fetch information for. Must be valid UUID.
Name Description account *api.Account All account information including wallet, device IDs and more. error error An optional error value if an error occurred.
account , err : = lg . AccountGetId ( ctx , "8f4d52c7-bf28-4fcf-8af2-1d4fcf685592" ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Get accounts error." ) return } logger . Info ( "Wallet is: %v" , account . Wallet )
AccountsGetId
Fetch information for multiple accounts by user IDs.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userIds []string REQUIRED Array of user IDs to fetch information for. Must be valid UUID.
Name Description account []*api.Account An array of accounts. error error An optional error value if an error occurred.
userIDs : = [ ] string { "9a51cf3a-2377-11eb-b713-e7d403afe081" , "a042c19c-2377-11eb-b7c1-cfafae11cfbc" } accounts , err : = lg . AccountsGetId ( ctx , userIDs ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Get accounts error." ) return } for _ , account : = range accounts { logger . Info ( "Wallet is: %v" , account . Wallet ) }
AccountUpdateId
Update an account by user ID.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userId string REQUIRED User ID for which the information is to be updated. Must be valid UUID. metadata map[string]interface REQUIRED The metadata to update for this account. username string REQUIRED Username to be set. Must be unique. Use "" if it is not being updated. displayName string REQUIRED Display name to be updated. Use "" if it is not being updated. timezone string REQUIRED Timezone to be updated. Use "" if it is not being updated. location string REQUIRED Location to be updated. Use "" if it is not being updated. language string REQUIRED Lang tag to be updated. Use "" if it is not being updated. avatarUrl string REQUIRED User's avatar URL. Use "" if it is not being updated.
Name Description error error An optional error value if an error occurred.
userID : = "4ec4f126-3f9d-11e7-84ef-b7c182b36521" username : = "" metadata : = make ( map [ string ] interface { } ) displayName : = "" timezone : = "" location : = "" langTag : = "" avatarUrl : = "" if err : = lg . AccountUpdateId ( ctx , userID , username , metadata , displayName , timezone , location , langTag , avatarUrl ) ; err != nil { logger . WithField ( "err" , err ) . Error ( "Account update error." ) }
Authenticate
AuthenticateApple
Authenticate user and create a session token using an Apple sign in token.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. token string REQUIRED Apple sign in token. username string REQUIRED The user's username. If left empty, one is generated. create bool REQUIRED Create user if one didn't exist previously.
Name Description userID string The user ID of the authenticated user. username string The username of the authenticated user. create bool Value indicating if this account was just created or already existed. error error An optional error value if an error occurred.
userid , username , created , err : = lg . AuthenticateApple ( ctx , "some-oauth-access-token" , "username" , true ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Authenticate apple error." ) }
AuthenticateCustom
Authenticate user and create a session token using a custom authentication managed by an external service or source not already supported by layerG.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. id string REQUIRED Custom ID to use to authenticate the user. Must be between 6-128 characters. username string The user's username. If left empty, one is generated. create bool REQUIRED Create user if one didn't exist previously.
Name Description userID string The user ID of the authenticated user. username string The username of the authenticated user. create bool Value indicating if this account was just created or already existed. error error An optional error value if an error occurred.
userid , username , created , err : = lg . AuthenticateCustom ( ctx , "48656C6C6F20776F726C64" , "username" , true ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Authenticate custom error." ) }
AuthenticateDevice
Authenticate user and create a session token using a device identifier.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. id string REQUIRED Device ID to use to authenticate the user. Must be between 1-128 characters. username string The user's username. If left empty, one is generated. create bool true Create user if one didn't exist previously.
Name Description userID string The user ID of the authenticated user. username string The username of the authenticated user. create bool Value indicating if this account was just created or already existed. error error An optional error value if an error occurred.
userid , username , created , err : = lg . AuthenticateDevice ( ctx , "48656C6C6F20776F726C64" , "username" , true ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Authenticate device error." ) }
AuthenticateEmail
Authenticate user and create a session token using an email address and password.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. email string REQUIRED Email address to use to authenticate the user. Must be between 10-255 characters. password string REQUIRED Password to set. Must be longer than 8 characters. username string The user's username. If left empty, one is generated. create bool true Create user if one didn't exist previously.
Name Description userID string The user ID of the authenticated user. username string The username of the authenticated user. create bool Value indicating if this account was just created or already existed. error error An optional error value if an error occurred.
userid , username , created , err : = lg . AuthenticateEmail ( ctx , "[email protected] " , "48656C6C6F20776F726C64" , "username" , true ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Authenticate email error." ) }
AuthenticateFacebook
Authenticate user and create a session token using a Facebook account token.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. token string REQUIRED Facebook OAuth or Limited Login (JWT) access token. import bool REQUIRED Whether to automatically import Facebook friends after authentication. username string The user's username. If left empty, one is generated. create bool REQUIRED Create user if one didn't exist previously.
Name Description userID string The user ID of the authenticated user. username string The username of the authenticated user. create bool Value indicating if this account was just created or already existed. error error An optional error value if an error occurred.
userid , username , created , err : = lg . AuthenticateFacebook ( ctx , "some-oauth-access-token" , true , "username" , true ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Authenticate facebook error." ) }
AuthenticateFacebookInstantGame
Authenticate user and create a session token using a Facebook Instant Game.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. playerInfo string REQUIRED Facebook Player info. username string The user's username. If left empty, one is generated. create bool REQUIRED Create user if one didn't exist previously.
Name Description userID string The user ID of the authenticated user. username string The username of the authenticated user. create bool error error An optional error value if an error occurred.
userid , username , created , err : = lg . AuthenticateFacebookInstantGame ( ctx , "player-info" , true , "username" , true ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Authenticate facebook error." ) }
AuthenticateGameCenter
Authenticate user and create a session token using Apple Game Center credentials.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. playerId string REQUIRED PlayerId provided by GameCenter. bundleId string REQUIRED BundleId of your app on iTunesConnect. timestamp int REQUIRED Timestamp at which Game Center authenticated the client and issued a signature. salt string REQUIRED A random string returned by Game Center authentication on client. signature string REQUIRED A signature returned by Game Center authentication on client. publicKeyUrl string REQUIRED A URL to the public key returned by Game Center authentication on client. username string The user's username. If left empty, one is generated. create bool REQUIRED Create user if one didn't exist previously.
Name Description userID string The user ID of the authenticated user. username string The username of the authenticated user. create bool Value indicating if this account was just created or already existed. error error An optional error value if an error occurred.
userid , username , created , err : = lg . AuthenticateGameCenter ( ctx , playerID , bundleID , timestamp , salt , signature , publicKeyUrl , username , create ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Authenticate game center error." ) }
AuthenticateGoogle
Authenticate user and create a session token using a Google ID token.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. token string REQUIRED Google OAuth access token. username string REQUIRED The user's username. If left empty, one is generated. create bool REQUIRED Create user if one didn't exist previously.
Name Description userID string The user ID of the authenticated user. username string The username of the authenticated user. create bool Value indicating if this account was just created or already existed. error error An optional error value if an error occurred.
userid , username , created , err : = lg . AuthenticateGoogle ( ctx , "some-id-token" , "username" , true ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Authenticate google error." ) }
AuthenticateSteam
Authenticate user and create a session token using a Steam account token.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. token string REQUIRED Steam token. username string The user's username. If left empty, one is generated. create bool true Create user if one didn't exist previously.
Name Description userID string The user ID of the authenticated user. username string The username of the authenticated user. create bool Value indicating if this account was just created or already existed. error error An optional error value if an error occurred.
userid , username , created , err : = lg . AuthenticateSteam ( ctx , "steam-token" , "username" , true ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Authenticate steam error." ) }
AuthenticateTokenGenerate
Generate a layerG session token from a user ID.
Name Default Description userId string REQUIRED User ID to use to generate the token. username string The user's username. If left empty, one is generated. expiresAt int UTC time in seconds when the token must expire. Defaults to server configured expiry time. vars map[string]string Extra information that will be bundled in the session token.
Name Description token string The layerG session token. validity int64 The period for which the token remains valid. error error An optional error value if an error occurred.
token , validity , err : = lg . AuthenticateTokenGenerate ( "user_id" , "username" , 0 ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Authenticate token generate error." ) return } logger . Info ( "Session token: %q, valid for %v seconds" , token , validity )
LinkApple
Link Apple authentication to a user ID.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userId string REQUIRED The user ID to be linked. token string REQUIRED Apple sign in token.
Name Description error error An optional error value if an error occurred.
err : = lg . LinkApple ( ctx , "some-oauth-access-token" , "userId" ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Link apple error." ) }
LinkCustom
Link custom authentication to a user ID.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userId string REQUIRED The user ID to be linked. customId string REQUIRED Custom ID to be linked to the user.
Name Description error error An optional error value if an error occurred.
err : = lg . LinkCustom ( ctx , "48656C6C6F20776F726C64" , "customId" ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Link custom error." ) }
LinkDevice
Link device authentication to a user ID.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userId string REQUIRED The user ID to be linked. deviceId string REQUIRED Device ID to be linked to the user.
Name Description error error An optional error value if an error occurred.
err : = lg . LinkDevice ( ctx , "48656C6C6F20776F726C64" , "deviceId" ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Link device error." ) }
LinkEmail
Link email authentication to a user ID.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userId string REQUIRED The user ID to be linked. email string REQUIRED Authentication email to be linked to the user. password string REQUIRED Password to set. Must be longer than 8 characters.
Name Description error error An optional error value if an error occurred.
err : = lg . LinkEmail ( ctx , "userId" , "[email protected] " , "password" , ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Link email error." ) }
LinkFacebook
Link Facebook authentication to a user ID.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userId string REQUIRED The user ID to be linked. username string If left empty, one is generated. token string REQUIRED Facebook OAuth or Limited Login (JWT) access token. importFriends bool REQUIRED Whether to automatically import Facebook friends after authentication.
Name Description error error An optional error value if an error occurred.
err : = lg . LinkFacebook ( ctx , "userId" , "some-oauth-access-token" , "username" , true ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Link facebook error." ) }
LinkFacebookInstantGame
Link Facebook Instant Game authentication to a user ID.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userId string REQUIRED The user ID to be linked. signedPlayerInfo string REQUIRED Facebook player info.
Name Description error error An optional error value if an error occurred.
err : = lg . LinkFacebookInstantGame ( ctx , "player-info" , "userId" ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Link facebook error." ) }
LinkGameCenter
Link Apple Game Center authentication to a user ID.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userId string REQUIRED The user ID to be linked. playerId string REQUIRED Player ID provided by Game Center. bundleId string REQUIRED Bundle ID of your app on iTunesConnect. timestamp int REQUIRED Timestamp at which Game Center authenticated the client and issued a signature. salt string REQUIRED A random string returned by Game Center authentication on client. signature string REQUIRED A signature returned by Game Center authentication on client. publicKeyUrl string REQUIRED A URL to the public key returned by Game Center authentication on client.
Name Description error error An optional error value if an error occurred.
err : = lg . LinkGameCenter ( ctx , userID , playerID , bundleID , timestamp , salt , signature , publicKeyUrl ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Link game center error." ) }
LinkGoogle
Link Google authentication to a user ID.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userId string REQUIRED The user ID to be linked. token string REQUIRED Google OAuth access token.
Name Description error error An optional error value if an error occurred.
err : = lg . LinkGoogle ( ctx , "userId" , "some-id-token" ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Link google error." ) }
LinkSteam
Link Steam authentication to a user ID.
Name Default Description ctx context.Context REQUIRED The context object represents information about the server and requester. userId string REQUIRED The user ID to be linked. username string If left empty, one is generated. token string REQUIRED Steam access token. importFriends bool REQUIRED Whether to automatically import Steam friends after authentication.
Name Description error error An optional error value if an error occurred.
err : = lg . LinkSteam ( ctx , "userId" , "steam-token" , "username" , true ) if err != nil { logger . WithField ( "err" , err ) . Error ( "Link steam error." ) }
UnlinkApple