Node API Reference
Constructor
Arguments
new Magic(secretApiKey, options?)
secretApiKey
(String): Your secret API Key retrieved from the Magic Dashboard.options.endpoint?
(String): A URL pointing to the Magic API. You should not need to provide this value except in case you are building highly custom integration tests.
Example
Token Module
The Token Module and its members are accessible on the Magic Admin SDK instance by the token
property.
getIssuer
Arguments
getIssuer(didToken)
didToken
(String): A Decentralized ID Token generated by a Magic user on the client-side.
Returns
string
: The Decentralized ID of the Magic User who generated the DID Token.
Example
getPublicAddress
Gets the cryptographic public address of the Magic User who generated the supplied token.
Arguments
getPublicAddress(didToken)
didToken
(String): A Decentralized ID Token generated by a Magic user on the client-side.
Returns
string
: The public address of the Magic User who generated the DID Token.
Example
decode
Decodes a Decentralized ID Token from a Base64 string into a tuple of its individual components.
Arguments
decode(didToken)
didToken
(String): A Decentralized ID Token generated by a Magic user on the client-side.
Returns
[string, Claim]
: containing the [proof, claim]
that composes the DID Token. The claim
, while given as a JSON string, is automatically parsed to the following interface:
As a convenience, the above interface is available to your code in TypeScript:
Example
validate
Validates a Decentralized ID token.
Arguments
validate(didToken, attachment? = 'none')
didToken
(String): A Decentralized ID Token generated by a Magic user on the client-side.attachment?
(String): Arbitrary, serialized data to be used for recovery of theadd
field on the Decentralized ID Token (Defaults to"none"
).
Returns
void
: The function will return if the specified DID token is authentic and not expired. If the token is forged or otherwise invalid, the function will throw a descriptive error.
Example
User Module
logoutByIssuer
Logs a user out of all client-side Magic SDK sessions given the user's Decentralized ID.
Arguments
logoutByIssuer(issuer)
issuer
(String): The user's Decentralized ID, which can be parsed usingTokenModule.getIssuer
.
Returns
Promise<void>
Example
logoutByPublicAddress
Logs a user out of all client-side Magic SDK sessions given the user's public address.
Arguments
logoutByPublicAddress(publicAddress)
publicAddress
(String): The user's public address to log out via the Magic API.
Returns
Promise<void>
Example
logoutByToken
Logs a user out of all client-side Magic SDK sessions given the user's public address.
Arguments
logoutByToken(didToken)
didToken
(String): A valid Decentralized ID Token generated client-side by a Magic user.
Returns
Promise<void>
Example
getMetadataByIssuer
Retrieves information about user by the supplied "issuer".
Arguments
getMetadataByIssuer(issuer)
issuer
(String): The user's Decentralized ID, which can be parsed usingTokenModule.getIssuer
.
Returns
Promise<{ issuer, email, publicAddress }>
: an object containing the issuer, email and cryptographic public address of the authenticated user.
issuer
(String): The Decentralized ID of the user. We recommend this value to be used as the user ID in your own tables.email
(String): Email address of the authenticated user.publicAddress
(String): The authenticated user's public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.
Example
getMetadataByPublicAddress
Retrieves information about user by the supplied public address.
Arguments
getMetadataByPublicAddress(publicAddress)
publicAddress
(String): The user's Ethereum public address, which can be parsed usingTokenModule.getPublicAddress
.
Returns
Promise<{ issuer, email, publicAddress }>
: an object containing the issuer, email and cryptographic public address of the authenticated user.
issuer
(String): The Decentralized ID of the user. We recommend this value to be used as the user ID in your own tables.email
(String): Email address of the authenticated user.publicAddress
(String): The authenticated user's public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.
Example
getMetadataByToken
Retrieves information about user by the supplied DID Token.
Arguments
getMetadataByToken(didToken)
didToken
(String): A valid Decentralized ID Token generated client-side by a Magic user.
Returns
Promise<{ issuer, email, publicAddress }>
: an object containing the issuer, email and cryptographic public address of the authenticated user.
issuer
(String): The Decentralized ID of the user. We recommend this value to be used as the user ID in your own tables.email
(String): Email address of the authenticated user.publicAddress
(String): The authenticated user's public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.
Example
Utils Module
The Utils Module and it's members are accessible on the Magic Admin SDK instance by the utils
property.
parseAuthorizationHeader
Arguments
parseAuthorizationHeader(header)
header
(String): A request authorization header passed in front the client-side application that looks likeBearer WyIweG...n0iXQ==
.
Returns
string
: The DID token embedded in the authorization header.
{% hint style="info" %} This is only available to Admin SDK version 1.1.0 and above. {% endhint %}
Example
Errors & Warnings
SDKError
The SDKError
class is exposed for instanceof
operations.
Additionally, an enumeration of relevant error codes is also exposed for convenience and human readability:
Error Codes
Enum Key | Description |
---|---|
TokenExpired | The supplied DID Token is invalid due to an expired timestamp. |
TokenCannotBeUsedYet | The nbf ("not before") field of the supplied DID Token is set in the future. By default, a 5 minute leeway is added to the timestamp. |
IncorrectSignerAddress | The supplied DID Token is invalid due to a mismatch between the signature origin and the claimed user public address. |
FailedRecoveryProof | The supplied DID Token could not be recovered using Elliptic Curve recovery. |
ApiKeyMissing | The API key required for a particular action is missing or malformed. |
MalformedTokenError | The supplied DID Token could not be successfully parsed. |
ServiceError | An error occurred while communicating with the Magic API. Be sure to check the data property of the error object for additional context. |
data
& Nested Errors
For additional context about an error, the SDKError.data
property contains an array of potentially illuminating metadata. At the moment, this property is reserved for service errors when communication to Magic APIs fail.