useGlyphTokenGate
(Available from v1.0.25)
A hook that checks token ownership, allowing you to gate specific features of your web app based on whether a user holds the required NFTs.
This hook resolves delegations (unless disabled) and NFT ownership for any user, regardless of the wallet provider being used. However, ownership resolution through linked wallets and their corresponding delegates is supported for Glyph users only.
Delegations are resolved on a per-network basis, so users must delegate their assets on the same network where the gating occurs for this feature to function as expected.
Import
tsx
import { useGlyphTokenGate } from "@use-glyph/sdk-react";Usage
tsx
function TokenGateComponent() {
const { checkTokenGate, isTokenGateLoading } = useGlyphTokenGate();
const [contractAddress, setContractAddress] = useState<string>("");
const handleTokenGate = async () => {
const tokenOwnership = await checkTokenGate({ contractAddress });
if (tokenOwnership.error) return alert(`Tokengate error: ${tokenOwnership.error}`);
alert(tokenOwnership.result ? "NFT owned" : "NFT not owned");
};
return (
<div className="flex flex-col items-center justify-center gap-4 mt-4 text-center">
<input
type="text"
placeholder="Contract address"
value={contractAddress}
onChange={(e) => setContractAddress(e.target.value)}
/>
<button
onClick={()=> handleTokenGate()}
disabled={isTokenGateLoading || !contractAddress}
>
{isTokenGateLoading ? "Checking..." : "Check token ownership"}
</button>
</div>
);
}Return Type
| Property | Type | Description |
|---|---|---|
| checkTokenGate | (req: GlyphOwnershipCheckRequest) => Promise<GlyphTokenGateResult> | Function to check token gating requirements |
| isTokenGateLoading | boolean | Loading state for the token gate check operation |
GlyphOwnershipCheckRequest
| Property | Type | Required | Description |
|---|---|---|---|
| contractAddress | string | Yes | The contract address of the NFT (ERC721 and ERC1155) to check for ownership. |
| chainId | number | No | The network ID to check ownership on. If not provided, uses the current connected network (typically Apechain). |
| quantity | number | No | Minimum quantity of tokens required for access (default: 1). |
| tokenIdRange | string | No | Specific token ID range to check (e.g., "1-100"). |
| includeDelegates | boolean | No | Whether to include delegated tokens in the ownership check (default: true). |
GlyphTokenGateResult
| Property | Type | Description |
|---|---|---|
| result | boolean | Whether the user meets the token gating requirements. |
| error | string | Error message if the check failed (only present when result is false). |
