API Reference

Constructor

The Magic class is the entry-point to the Magic SDK. It must be instantiated with a Magic publishable key.

Magic

Public constructors
Magic(context: Context, apiKey: String)Construct a Magic instance with publishable API Key retrieved from the Magic Dashboard
Magic(context:Context, apiKey: String, network: Magic.EthNetwork)Construct a Magic instance with publishable Key and Ethereum network
Magic(context:Context, apiKey: String, customNode: CustomNodeConfiguration)Construct a Magic instance with publishable Key and Custom Node configuration

Example

import link.magic.android.Magic
open class MagicActivity: AppCompatActivity() {
lateinit var magic: Magic
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
magic = Magic(this, "YOUR_PUBLISHABLE_KEY")
}
}

Auth module

The Auth Module and its members are accessible on the Magic SDK instance by the auth property.

import link.magic.android.Magic
var magic: Magic
magic.auth;
magic.auth.loginWithMagicLink;

loginWithMagicLink

Public Methods
Methods
loginWithMagicLink(configuration: LoginWithMagicLinkConfiguration): CompletableFuture<DIDToken>
Returns

DIDToken: Response<String>()

The function resolves upon authentication request success and rejects with a specific error code if the request fails. The resolved value is a Decentralized ID token with a default 15-minute lifespan.

Example
class MagicActivity: AppCompatActivity() {
lateinit var magic: Magic
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
magic = Magic(this, "YOUR_PUBLISHABLE_API_KEY")
}
fun login(v: View) {
val email = findViewById<EditText>(R.id.emailInput)
val configuration = LoginWithMagicLinkConfiguration(email.text.toString())
val completable = magic.auth.loginWithMagicLink(configuration)
// Logging in
completable.whenComplete { response: DIDToken?, error: Throwable? ->
if (error != null) {
// Handle error
}
if (response != null && !response.hasError()) {
Log.d("Magic", "You're logged in!" + response.result)
} else {
Log.d("Magic", "Not Logged in")
}
}
}
}
Associated Class

LoginWithMagicLinkConfiguration(showUI: Boolean? = true, email: String)

  • email The user email to log in with.
  • showUI If true, show an out-of-the-box pending UI while the request is in flight.

User Module

The User Module and its members are accessible on the Magic SDK instance by the user property.

import MagicSDK
let magic = Magic.shared
magic.user
magic.user.getIdToken
magic.user.generateIdToken
magic.user.getMetadata
magic.user.updateEmail
magic.user.isLoggedIn
magic.user.logout

updateEmail

Initiates the update email flow that allows a user to change to a new email

Public Methods
Methods
updateEmail(configuration: UpdateEmailConfiguration) -> CompletableFuture<UpdateEmailResponse>
Returns

UpdateEmailResponse: Response<Boolean>()

The Completable resolves with a true boolean value if update email is successful and rejects with a specific error code if the request fails.

Example
class MagicActivity: AppCompatActivity() {
lateinit var magic: Magic
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
magic = Magic(this, "YOUR_PUBLISHABLE_KEY")
}
// ⭐️ Assuming user is logged in
fun updateEmail(v: View) {
val configuration = UpdateEmailConfiguration("new_user_email@example.com")
val completable = magic.user.updateEmail(configuration)
completable.whenComplete { response: UpdateEmailResponse?, error: Throwable? ->
if (response != null) {
Log.d("Magic", response.result.toString()) // "True"
} else {
// handle error
}
}
}
}
Associated Class

UpdateEmailConfiguration(showUI: Boolean? = true, email: String)

  • email The user email to update with.
  • showUI If true, show an out-of-the-box pending UI while the request is in flight.

getIdToken

Generates a Decentralized Id Token which acts as a proof of authentication to resource servers.

Public Methods
Methods
getIdToken(configuration: GetIdTokenConfiguration?): CompletableFuture<GetIdTokenResponse>
Returns

GetIdTokenResponse: Response<String>()

The Completable resolves with a true boolean value if update email is successful and rejects with a specific error code if the request fails.

class MagicActivity: AppCompatActivity() {
lateinit var magic: Magic
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
magic = Magic(this, "YOUR_PUBLISHABLE_KEY")
}
// ⭐️Assuming user is logged in
fun getIdToken(v: View) {
val configuration = GetIdTokenConfiguration(lifespan = 900)
val completable = magic.user.getIdToken(configuration)
completable.whenComplete { response: GetIdTokenResponse?, error: Throwable? ->
if (response != null) {
Log.d("Magic", response.result)
} else {
// handle Error
}
}
}
}
Associated Class

GetIdTokenConfiguration(lifespan: Long? = 900)

  • lifespan? : will set the lifespan of the generated token. Defaults to 900s (15 mins)

generateIdToken

Generates a Decentralized Id Token with optional serialized data.

Public Methods
Methods
generateIdToken(configuration: GenerateIdTokenConfiguration?): CompletableFuture<GenerateIdTokenResponse>
Returns

GenerateIdTokenResponse: Response<String>()

Base64-encoded string representation of a JSON tuple representing [proof, claim]

Example
class MagicActivity: AppCompatActivity() {
lateinit var magic: Magic
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
magic = Magic(this, "YOUR_PUBLISHABLE_KEY")
}
// ⭐️Assuming user is logged in
fun generateIdToken(v: View) {
val configuration = GenerateIdTokenConfiguration(lifespan = 900, attachment = "none")
val completable = magic.user.generateIdToken(configuration)
completable.whenComplete { response: GenerateIdTokenResponse?, error: Throwable? ->
if (response != null) {
Log.d("Magic", response.result)
} else {
// handle Error
}
}
}
}
Associated Class

GenerateIdTokenConfiguration(attachment: String? = "none", lifespan: Long? = 900)

  • lifespan : will set the lifespan of the generated token. Defaults to 900s (15 mins)
  • attachment : will set a signature of serialized data in the generated token. Defaults to "none"

getMetadata

Retrieves information for the authenticated user.

Public Methods
Methods
getMetadata(): CompletableFuture<GetMetadataResponse>
Returns

GetMetadataResponse: Response<UserMetadataResponse>()

The Completable containing the issuer, email and cryptographic public address of the authenticated user.

class UserMetadataResponse {
var issuer: String? = null
var publicAddress: String? = null
var email: String? = null
}
  • issuer : The Decentralized ID of the user. In server-side use-cases, we recommend this value to be used as the user ID in your own tables.
  • email : Email address of the authenticated user.
  • publicAddress: The authenticated user's public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.
Example
class MagicActivity: AppCompatActivity() {
lateinit var magic: Magic
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
magic = Magic(this, "YOUR_PUBLISHABLE_KEY")
}
// ⭐️Assuming user is logged in
fun getMetadata(v: View) {
val completable = magic.user.getMetadata()
completable.whenComplete { response: GetMetadataResponse?, error: Throwable? ->
if (response != null) {
Log.d("Magic", "Email: " + response.result.email)
Log.d("Magic", "Issuer: " + response.result.issuer)
} else {
// handle Error
}
}
}
}

isLoggedIn

Checks if a user is currently logged in to the Magic SDK.

Public Methods
Methods
isLoggedIn(): CompletableFuture<IsLoggedInResponse>
Returns

IsLoggedInResponse: Response<Boolean>()

Example
class MagicActivity: AppCompatActivity() {
lateinit var magic: Magic
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
magic = Magic(this, "YOUR_PUBLISHABLE_KEY")
}
// ⭐️ Assuming user is logged in
fun isLoggedIn(v: View) {
val completable = magic.user.isLoggedIn()
completable.whenComplete { response: IsLoggedInResponse?, error: Throwable? ->
if (response != null && response.result) {
Log.d("Magic", "You're Logged In")
}
if (error != null) {
// handle Error
}
}
}
}

logout

Logs out the currently authenticated Magic user

Public Methods
Methods
logout(): CompletableFuture<LogoutResponse>
Returns

LogoutResponse: Response<Boolean>()

Example
class MagicActivity: AppCompatActivity() {
lateinit var magic: Magic
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
magic = Magic(this, "YOUR_PUBLISHABLE_KEY")
}
// ⭐️Assuming user is logged in
fun logout(v: View) {
val completable = magic.user.logout()
completable.whenComplete { response: LogoutResponse?, error: Throwable? ->
if (response != null && response.result) {
Log.d("Magic", "You're logged out!")
}
if (error != null) {
// handle Error
}
}
}
}