Skip to main content

Overview

The Magic package for Python is your entry-point to secure, passwordless authentication for your application. This guide will cover some important topics for getting started with Magic Python package 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 Python SDK requires Python 3.11 or higher.

Constructor

Magic()

Initialization

Initialize Magic instance.
Python

Token Resource

The token resource and its methods are accessible on the Magic instance by the Token attribute. It provides methods to interact with the DID Token.
The token resource does not make any API calls to the Magic server.

get_issuer

Extracts the iss from the DID Token.
Python
Arguments
  • did_token (str): A DID Token generated by a Magic User on the client-side
Raises
  • DIDTokenMalformed if the given DID Token is malformed
Returns
  • A Decentralized ID (iss) of the Magic user who generated the DID Token

get_public_address

Gets the cryptographic public address of the Magic User who generated the supplied DID Token.
Python
Arguments
  • did_token (str): A DID Token generated by a Magic user on the client-side
Raises
  • DIDTokenMalformed if the given DID Token is malformed
Returns
  • A 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.
Python
Arguments
  • did_token (str): A DID Token generated by a Magic user on the client-side
Raises
  • DIDTokenMalformed if the given DID Token is malformed
Returns
  • proof (str): A digital signature that proves the validity of the given claim
  • claim (dict): Unsigned data the user asserts. This should equal the proof after Elliptic Curve recovery. See Decentralized ID Token Specification for fields inside the claim.

validate

Validates a DID token.
Python
Arguments
  • did_token (str): A DID Token generated by a Magic user on the client-side
Raises
  • DIDTokenMalformed if the given DID Token is malformed
  • DIDTokenInvalid if the given DID Token is invalid
Returns
  • None.

User Resource

The user resource and its methods are accessible on the Magic instance by the User attribute. It provides methods to interact with the User.

get_metadata_by_email

Retrieves information about the user by the supplied email address. Resolves both email-based and Google OAuth users. When both an email user and an OAuth user share the same email (apps without Google autolinking), the email user is returned by default; pass provider to explicitly target an OAuth user.
Python
Arguments
  • email (str): The user’s email address
  • provider (str, optional): Optional OAuth provider filter (e.g. "google"). When set, the lookup targets the OAuth user with this email and provider directly, bypassing the default email lookup.
Raises
  • RateLimitingError: If you have sent too many requests within a given period of time
  • BadRequestError: If the supplied parameters are invalid
  • AuthenticationError: If your API secret key cannot be authenticated with Magic API server
  • ForbiddenError: If your API secret key is not authorized to access the resources
  • APIError: For any other API error
  • APIConnectionError: If your server cannot communicate with the Magic server. Normally this is a network communication error.
See Error Handling for more examples.
Returns
  • A MagicResponse: The data field contains all of the user meta information.
    • email (str): The user’s email address
    • phone_number (str): The user’s phone number
    • subject (str): The federated identity subject, if any
    • oauth_provider (str): OAuth provider, if any
    • wallets (arr): Array of the user’s wallets
Example
Python

get_metadata_by_issuer

Retrieves information about the user by the supplied iss from the DID Token. This method is useful if you store the iss with your user data, which is recommended.
Python
Arguments
  • issuer (str): The user’s Decentralized ID, which can be parsed using Token.get_issuer
Raises
  • RateLimitingError: If you have sent too many requests within a given period of time
  • BadRequestError: If the supplied parameters are invalid
  • AuthenticationError: If your API secret key cannot be authenticated with Magic API server
  • ForbiddenError: If your API secret key is not authorized to access the resources
  • APIError: For any other API error
  • APIConnectionError: If your server cannot communicate with the Magic server. Normally this is a network communication error.
See Error Handling for more examples.
Returns
  • A MagicResponse: The data field contains all of the user meta information.
    • issuer (str): The user’s Decentralized ID
    • public_address (str): The authenticated user’s public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.
    • email (str): The user’s email address
    • phone_number (str): The user’s phone number
    • oauth_provider (str): OAuth provider, if any
    • wallets (arr): Array of user’s wallet addresses

get_metadata_by_public_address

Retrieves information about the user by the supplied public_address. This method is useful if you store the public_address with your user data.
Python
Arguments Raises
  • RateLimitingError: If you have sent too many requests within a given period of time.
  • BadRequestError: If the supplied parameters are invalid
  • AuthenticationError: If your API secret key cannot be authenticated with Magic API server
  • ForbiddenError: If your API secret key is not authorized to access the resources
  • APIError: For any other API error
  • APIConnectionError: If your server cannot communicate with the Magic server. Normally this is a network communication error.
See Error Handling for more examples.
Returns
  • A MagicResponse: The data field contains all of the user meta information.
    • issuer (str): The user’s Decentralized ID
    • public_address (str): The authenticated user’s public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.
    • email (str): The user’s email address
    • phone_number (str): The user’s phone number
    • oauth_provider (str): OAuth provider, if any
    • wallets (arr): Array of user’s wallet addresses

get_metadata_by_public_addresses

Retrieves metadata for a batch of users by their Ethereum public addresses in a single request. This method is useful when you need to look up information for a large number of users, as it avoids the rate limiting issues you would encounter by calling get_metadata_by_public_address in a loop.
Python
Arguments
  • public_addresses (arr): Array of Ethereum public addresses. Maximum of 500 addresses per request.
Raises
  • RateLimitingError: If you have sent too many requests within a given period of time.
  • BadRequestError: If the batch size exceeds 500 addresses, or if any other supplied parameters are invalid.
  • AuthenticationError: If your API secret key cannot be authenticated with Magic API server
  • ForbiddenError: If your API secret key is not authorized to access the resources
  • APIError: For any other API error
  • APIConnectionError: If your server cannot communicate with the Magic server. Normally this is a network communication error.
See Error Handling for more examples.
Returns
  • A MagicResponse: The data field contains:
    • users (list): Successfully resolved users. Each entry contains:
      • issuer (str): The user’s Decentralized ID
      • public_address (str): The user’s Ethereum public address
      • email (str): The user’s email address
      • phone_number (str): The user’s phone number
      • oauth_provider (str): OAuth provider, if any
      • subject (str): Federated identity subject, if any
      • username (str): WebAuthn username, if any
      • wallets (arr): Array of user’s wallet addresses
    • errors (dict): A mapping of public_address → error message for any addresses that could not be resolved (e.g. user not found, address belongs to a different app, or malformed address). Successfully resolved addresses are not present in this dict.

get_metadata_by_token

Retrieves information about the user by the supplied DID Token.
Python
Arguments
  • did_token (str): A DID Token generated by a Magic User on the client-side.
Raises
  • RateLimitingError: If you have sent too many requests within a given period of time
  • BadRequestError: If the supplied parameters are invalid
  • AuthenticationError: If your API secret key cannot be authenticated with Magic API server
  • ForbiddenError: If your API secret key is not authorized to access the resources
  • APIError: For any other API error
  • APIConnectionError: If your server cannot communicate with the Magic server. Normally this is a network communication error.
See Error Handling for more examples.
Returns
  • A MagicResponse: The data field contains all of the user meta information.
    • issuer (str): The user’s Decentralized ID
    • public_address (str): The authenticated user’s public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.
    • email (str): The user’s email address
    • phone_number (str): The user’s phone number
    • oauth_provider (str): OAuth provider, if any
    • wallets (arr): Array of user’s wallet addresses

logout_by_issuer

Logs a user out of all Magic SDK sessions given the user’s Decentralized ID (iss). This method is useful if you store the iss with your user data, which is recommended.
Python
Arguments
  • issuer (str): The user’s Decentralized ID, which can be parsed using Token.get_issuer
Raises
  • RateLimitingError: If you have sent too many requests within a given period of time
  • BadRequestError: If the supplied parameters are invalid
  • AuthenticationError: If your API secret key cannot be authenticated with Magic API server
  • ForbiddenError: If your API secret key is not authorized to access the resources
  • APIError: For any other API error
  • APIConnectionError: If your server cannot communicate with the Magic server. Normally this is a network communication error.
See Error Handling for more examples.
Returns

logout_by_public_address

Logs a user out of all Magic SDK sessions given the user’s public address. This method is useful if you store the public_address.
Python
Arguments
  • public_address (str): The user’s Ethereum public address
Raises
  • RateLimitingError: If you have sent too many requests within a given period of time
  • BadRequestError: If the supplied parameters are invalid
  • AuthenticationError: If your API secret key cannot be authenticated with Magic API server
  • ForbiddenError: If your API secret key is not authorized to access the resources
  • APIError: For any other API error
  • APIConnectionError: If your server cannot communicate with the Magic server. Normally this is a network communication error.
See Error Handling for more examples.
Returns

logout_by_token

Logs a user out of all Magic SDK sessions given the DID Token.
Python
Arguments
  • did_token (str): A DID Token generated by a Magic user on the client-side
Raises
  • RateLimitingError: If you have sent too many requests within a given period of time
  • BadRequestError: If the supplied parameters are invalid
  • AuthenticationError: If your API secret key cannot be authenticated with Magic API server
  • ForbiddenError: If your API secret key is not authorized to access the resources
  • APIError: For any other API error
  • APIConnectionError: If your server cannot communicate with the Magic server. Normally this is a network communication error.
See Error Handling for more examples.
Returns

remove_mfa_by_public_address

Removes (deactivates) all multi-factor authentication (MFA) for the user with the supplied public_address. This is useful for building your own support flows—for example, helping a user who has lost access to their authenticator app regain access to their account. All active MFA factors for the user are deactivated, matching the behavior of disabling MFA from the Magic Dashboard.
Available in magic-admin 2.5.0 and later. The user must belong to the app associated with your API secret key; otherwise the request is rejected with a ForbiddenError.
Python
Arguments Raises
  • RateLimitingError: If you have sent too many requests within a given period of time
  • BadRequestError: If the supplied parameters are invalid, or no user is found for the supplied public address
  • AuthenticationError: If your API secret key cannot be authenticated with Magic API server
  • ForbiddenError: If your API secret key is not authorized to access the resources
  • APIError: For any other API error
  • APIConnectionError: If your server cannot communicate with the Magic server. Normally this is a network communication error.
See Error Handling for more examples.
Returns Example
Python

Utils Resource

The utils resource provides utility methods for common operations like parsing authorization headers and validating token ownership for NFT gating.

parse_authorization_header

Extracts a DID token from an HTTP Authorization header.
Python
Arguments
  • header (str): The Authorization header string in Bearer {token} format
Raises
  • ExpectedBearerStringError: If header is not in the expected format
Returns
  • str: The DID token extracted from the header
Example
Python

validate_token_ownership

Validates that a user owns a specific NFT token for token gating functionality.
Python
Arguments
  • did_token (str): The DID token to validate
  • contract_address (str): The smart contract address of the NFT
  • contract_type (str): Either 'ERC721' or 'ERC1155'
  • rpc_url (str): The RPC endpoint URL for the blockchain
  • token_id (str, optional): Required for ERC1155 contracts
Raises
  • ValueError: If ERC1155 is specified without token_id
  • Exception: If DID token validation fails
Returns
  • dict: Response with validation result
    • valid (bool): Whether the user owns the token
    • error_code (str): Error code if validation fails
    • message (str): Human-readable error message
Example
Python

WalletType Enum

The WalletType enum provides support for multiple blockchain networks. Supported Networks
  • EVM Compatible: ETH, HEDERA
  • Non-EVM: SOLANA, COSMOS

Multi-Chain User Metadata

The User resource supports retrieving metadata for specific wallet types across different blockchains.

get_metadata_by_issuer_and_wallet

Retrieves user metadata for a specific wallet type by issuer.
Python
Arguments
  • issuer (str): The user’s Decentralized ID
  • wallet_type (WalletType): The specific wallet type to query
Returns
  • A MagicResponse: The data field contains user metadata for the specified wallet type

get_metadata_by_public_address_and_wallet

Retrieves user metadata for a specific wallet type by public address.
Python
Arguments
  • public_address (str): The user’s public address
  • wallet_type (WalletType): The specific wallet type to query
Returns
  • A MagicResponse: The data field contains user metadata for the specified wallet type

get_metadata_by_token_and_wallet

Retrieves user metadata for a specific wallet type by DID token.
Python
Arguments
  • did_token (str): A DID Token generated by a Magic user
  • wallet_type (WalletType): The specific wallet type to query
Returns
  • A MagicResponse: The data field contains user metadata for the specified wallet type
Example
Python

Response and Error Handling

Response

There is only one response object that will be returned from a successful API call.

MagicResponse

This is the interface to interact Magic API responses. It will only be returned if the API request status code is between 200 (inclusive) and 300 (exclusive). You will have access to the following attributes:
  • content (bytes): Raw content returned by the API response
  • status_code (num): HTTP status code for the given request
  • data (dict): Parsed content
Python

Errors

The conventional HTTP response is adopted by the SDK. For the status code in:
  • 2XX - Indicates success
  • 4XX - Indicates client errors. Information provided to the SDK is invalid.
  • 5XX - Indicates server errors
Below is the error class inheritance which can help developers to programmatically handle the error cases.

MagicError

This is the base class of all the Magic SDK errors.
Python

RequestError

This is the base class of all the Magic API request errors. This error class will provide details of unsuccessful API requests.
Python

DIDTokenInvalid

This means the given token fails the validation.

DIDTokenMalformed

This means the given token format is invalid.

DIDTokenExpired

This means the given token has expired.

ExpectedBearerStringError

This means the Authorization header is not in the expected Bearer {token} format.

Error Handling

It is recommended to implement error handling for API responses.
Python

Resources