Overview
The Magic SDK for Node is your entry-point to secure, passwordless authentication for your application. This guide will cover some important topics for getting started with Node APIs and to make the most of Magic’s features.Getting Started
The Magic class is the entry-point to the Magic SDK. It must be instantiated with a Magic secret key.Installation
The Magic Admin Node SDK requires Node.js 18 or higher.Constructor
Magic.init()
Parameter | Type | Definition |
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. |
options.clientId? | String | Your Magic client ID. If not provided, will be fetched automatically. |
Initialization
Node.js
Token Module
The Token Module and its members are accessible on the Magic Admin SDK instance by thetoken
property.
The token module does not make any API calls to the Magic server.
getIssuer
Extracts the iss
from the DID Token.
Node.js
didToken
(String): A Decentralized ID Token generated by a Magic user on the client-side.
MalformedTokenError
: If the given DID Token is malformed
- A Decentralized ID (
iss
) of the Magic user who generated the DID Token
getPublicAddress
Gets the cryptographic public address of the Magic User who generated the supplied DID Token.
Node.js
didToken
(String): A Decentralized ID Token generated by a Magic user on the client-side
MalformedTokenError
: If the given DID Token is malformed
- The public address of the Magic User who generated the DID Token. Currently, this value is associated with the Ethereum blockchain.
decode
Decodes a DID Token from a Base64 string into a tuple of its individual components: proof
and claim
. This method allows you decode the DID Token and inspect the token. You can apply your own rules and validations on top of the current Token.validate method.
Node.js
didToken
(String): A Decentralized ID Token generated by a Magic user on the client-side
MalformedTokenError
: If the given DID Token is malformed
proof
(String): A digital signature that proves the validity of the givenclaim
claim
(Object): Unsigned data the user asserts. This should equal theproof
after Elliptic Curve recovery. See Decentralized ID Token Specification for fields inside theclaim
.
claim
is automatically parsed to the following interface:
Node.js
Node.js
validate
Validates a DID token.
Node.js
didToken
(String): A Decentralized ID Token generated by a Magic user on the client-sideattachment?
(String): Arbitrary, serialized data to be used for recovery of theadd
field on the Decentralized ID Token (Defaults to"none"
)
MalformedTokenError
: If the given DID Token is malformedTokenExpired
: If the given DID Token has expiredIncorrectSignerAddress
: If the given DID Token is invalid
Promise<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.
Users Module
getMetadataByIssuer
Retrieves information about user by the supplied “issuer”.
Arguments
issuer
(String): The user’s Decentralized ID, which can be parsed usingTokenModule.getIssuer
- A
MagicUserMetadata
: Thedata
field contains all of the user meta information.issuer
(String): The user’s Decentralized IDpublicAddress
(String): The authenticated user’s public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.email
(String): The user’s email addressphoneNumber
(String | null): The user’s phone numberoauthProvider
(String | null): OAuth provider, if anywallets
(Object[]): The multichain wallets of the authenticated user
getMetadataByPublicAddress
Retrieves information about user by the supplied public address.
Arguments
publicAddress
(String): The user’s Ethereum public address, which can be parsed usingTokenModule.getPublicAddress
- A
MagicUserMetadata
: Thedata
field contains all of the user meta information.issuer
(String): The user’s Decentralized IDpublicAddress
(String): The authenticated user’s public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.email
(String): The user’s email addressphoneNumber
(String | null): The user’s phone numberoauthProvider
(String | null): OAuth provider, if anywallets
(Object[]): The multichain wallets of the authenticated user
getMetadataByToken
Retrieves information about user by the supplied DID Token.
Arguments
didToken
(String): A valid Decentralized ID Token generated client-side by a Magic user
- A
MagicUserMetadata
: Thedata
field contains all of the user meta information.issuer
(String): The user’s Decentralized IDpublicAddress
(String): The authenticated user’s public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.email
(String): The user’s email addressphoneNumber
(String | null): The user’s phone numberoauthProvider
(String | null): OAuth provider, if anywallets
(Object[]): The multichain wallets of the authenticated user
logoutByIssuer
Logs a user out of all client-side Magic SDK sessions given the user’s Decentralized ID.
Arguments
issuer
(String): The user’s Decentralized ID, which can be parsed usingTokenModule.getIssuer
Promise<void>
logoutByPublicAddress
Logs a user out of all client-side Magic SDK sessions given the user’s public address.
Arguments
publicAddress
(String): The user’s Ethereum public address to log out via the Magic API.
Promise<void>
logoutByToken
Logs a user out of all client-side Magic SDK sessions given the user’s DID token.
Arguments
didToken
(String): A valid Decentralized ID Token generated client-side by a Magic user
Promise<void>
Multichain Methods
The following multichain methods require the use of the enumWalletType
that is exported from the Magic Admin SDK.
Supported Networks
- EVM Compatible: ETH, AVAX, CELO, HARMONY, ICON, ZILLIQA, HEDERA, CONFLUX, TERRA, TAQUITO, ED
- Non-EVM: SOLANA, POLKADOT, NEAR, FLOW, TEZOS, COSMOS, ALGOD, BITCOIN, HELIUM
getMetadataByIssuerAndWallet
Retrieves information about user, including their multichain wallets, by the supplied issuer.
Arguments
issuer
(String): The user’s Decentralized ID, which can be parsed usingTokenModule.getIssuer
walletType
(WalletType): The wallet type. Must importWalletType
from Admin SDK.
- A
MagicUserMetadata
: Thedata
field contains all of the user meta information.issuer
(String): The user’s Decentralized IDpublicAddress
(String): The authenticated user’s public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.email
(String): The user’s email addressphoneNumber
(String | null): The user’s phone numberoauthProvider
(String | null): OAuth provider, if anywallets
(Object[]): The multichain wallets of the authenticated user
getMetadataByPublicAddressAndWallet
Retrieves information about user, including their multichain wallets, by the supplied public address.
Arguments
publicAddress
(String): The user’s Ethereum public address, which can be parsed usingTokenModule.getPublicAddress
walletType
(WalletType): The wallet type. Must importWalletType
from Admin SDK.
- A
MagicUserMetadata
: Thedata
field contains all of the user meta information.issuer
(String): The user’s Decentralized IDpublicAddress
(String): The authenticated user’s public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.email
(String): The user’s email addressphoneNumber
(String | null): The user’s phone numberoauthProvider
(String | null): OAuth provider, if anywallets
(Object[]): The multichain wallets of the authenticated user
getMetadataByTokenAndWallet
Retrieves information about user, including their multichain wallets, by the supplied DID Token.
Arguments
didToken
(String): A valid Decentralized ID Token generated client-side by a Magic userwalletType
(WalletType): The wallet type. Must importWalletType
from Admin SDK.
- A
MagicUserMetadata
: Thedata
field contains all of the user meta information.issuer
(String): The user’s Decentralized IDpublicAddress
(String): The authenticated user’s public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.email
(String): The user’s email addressphoneNumber
(String | null): The user’s phone numberoauthProvider
(String | null): OAuth provider, if anywallets
(Object[]): The multichain wallets of the authenticated user
Node.js
Utils Module
The Utils Module and its members are accessible on the Magic Admin SDK instance by theutils
property.
parseAuthorizationHeader
Extracts a DID token from an HTTP Authorization header.
Arguments
header
(String): A request authorization header passed in from the client-side application that looks likeBearer WyIweG...n0iXQ==
string
: The DID token embedded in the authorization header
Node.js
validateTokenOwnership
Validates that a user owns a specific NFT token for token gating functionality.
Arguments
didToken
(String): The DID token to validatecontractAddress
(String): The smart contract address of the NFTcontractType
(String): Either'ERC721'
or'ERC1155'
rpcURL
(String): The RPC endpoint URL for the blockchaintokenId
(String, optional): Required for ERC1155 contracts
Promise<Object>
: Response with validation resultvalid
(Boolean): Whether the user owns the tokenerror_code
(String): Error code if validation failsmessage
(String): Human-readable error message
Node.js
Errors & Warnings
SDKError
The SDKError
class is exposed for instanceof
operations and provides structured error handling.
Node.js
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. |
ExpectedBearerString | The supplied DID token needs to be in the proper format of Bearer {token} |
AudienceMismatch | Audience does not match client ID. Please ensure your secret key matches the application which generated the DID token. |
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.