C#
Effortless SDK Integration: Mastering Wallets, NFTs and Payments with C#
SDK | Version | Package | Function reference | Object reference |
---|---|---|---|---|
C# | 2.0.74 | Download link | Documentation Link | Documentation Link |
This library should only be used server-side! In order to use the API, a
Client-ID
andClient-secret
will be required. Which can be obtained via our Developer Portal.
The following paragraphs provide examples of SDK initialization and usage. These code snippets assume you've already obtained the library from NuGet. Towards the end, you'll discover a practical code example illustrating how to create and retrieve wallets and NFTs using this C# SDK.
Initialising the API
string client_id = "YOUR_CLIENT_ID";
string client_secret = "YOUR_CLIENT_SECRET"; //This will either be a Sandbox or Production Secret
eVyEnvironment env = eVyEnvironment.Sandbox; //Depends on your client secret
//API Provider (Handles the communication)
var provider = new DefaultServerProvider(client_id, client_secret);
//API Initialization
var result = await VenlyAPI.Initialize(provider, env);
if (result.Success) Console.WriteLine("Venly API Initialized!");
else throw result.Exception;
After the initialisation you can now start using functions from the exposed API products, Wallet, NFT, and Pay.
Processing Client Requests
The logic to process incoming requests from clients is also part of the SDK. Request coming from clients like Unity3D or Unreal Engine (using the SDK) will be of type VyRequestData
. You can use the HandleRequest
function from the Backend namespace to produce a fitting VyServerResponseDto
that can be sent back to the Client.
//Optional, configure client access (allow/deny certain endpoints)
//ExecuteNativeTokenTransfer is by default not allowed to be called from the client
//but, you can opt to allow it anyway (pincode is required anyway)
VenlyAPI.Backend.AllowEndpoint(VenlyAPI.Wallet.IDs.ExecuteNativeTokenTransfer);
//Process Client Requests
VyServerResponseDto response = await VenlyAPI.Backend.HandleRequest(incoming_request).AwaitResult();
//Return response to the client...
Handling Extension Requests
On the client side it is possible to make custom requests (Extension Requests). These Extensions offer a way to easily invoke server-side logic by calling a specific Extension function and providing it with optional content. Of course, the server must implement a mechanism to handle these custom requests, that's where Request Handlers come into play (VyRequestHandlerBase
). The handler can be passed to the provider, and every extension request handled by the Backend.HandleRequest
function will be relayed to your custom Request handler.
Custom Request Handler
internal class CustomRequestHandler : VyRequestHandlerBase
{
public override VyTask<VyServerResponseDto> HandleExtension(VyRequestData requestData, object customData = null)
{
//Retrieve Extension Parameters
var extensionName = requestData.Uri;
var extensionData = requestData.ContentStr;
//Process the Extension Request...
//Create a Response
var response = new VyServerResponseDto
{
Success = true,
Data = "Give it some content",
ErrorMessage = null,
StatusCode = 200
};
//Return the Response
return VyTask<VyServerResponseDto>.Succeeded(response);
}
}#
Registering the Handler
//API Provider (Handles the communication)
var customHandler = new CustomRequestHandler();
var provider = new DefaultServerProvider(client_id, client_secret, customHandler);
//Initialize the Venly API...
//Extension Request will now automatically be relayed to your CustomRequestHandler
//if you are using Backend.HandleRequest of course
Server-Side Usage Examples
Here's a brief code example that showcases the combined usage of the C# SDK with the Wallet API and NFT API for tasks like wallet creation and retrieval, accessing NFT contracts, and minting NFTs.
//Retrieve Wallets (WALLET API)
//****************
var wallets = await VenlyAPI.Wallet.GetWallets().AwaitResult();
Console.WriteLine($"{wallets.Length} wallet(s) retrieved\n");
////Or without await
////++++++++++++++++
//VenlyAPI.Wallet.GetWallets()
// .OnComplete(result => { ... })
// .OnSuccess(wallets => { ... })
// .OnFail(ex => ...)
// .Finally(()=>...);
//Create User (WALLET API)
//***********
var userSigningPin = "123456"
var createUserParams = new VyCreateUserDto
{
Reference = "my_user_reference",
SigningMethod = new VyCreatePinSigningMethodRequest
{
Value = userSigningPin
};
};
var newUser = await VenlyAPI.Wallet.CreateUser(createUserParams).AwaitResult();
Console.WriteLine($"New user created, id={newUser.Id}");
//User Authentication
var userSigningMethodId = newUser.SigningMethods.First().Id;
var userAuth = new VyAuthSigningMethod(userSigningMethodId, userSigningPin);
//Create Wallet for User (WALLET API)
//**********************
var createWalletParams = new VyCreateWalletRequest
{
Chain = eVyChain.Matic,
Description = "DEMO wallet from Examples Snippet",
Identifier = "NEW-WALLET-TEST",
UserId = newUser.Id
};
var newWallet = await VenlyAPI.Wallet.CreateWallet(createWalletParams, userAuth).AwaitResult();
Console.WriteLine($"New wallet created for user ({newWallet.Chain}), id={newWallet.Id}");
//Retrieve Tokens For Specific Wallet (WALLET API)
//***********************************
if (wallets.Length > 0)
{
var walletData = wallets[0];
var nftTokens = await VenlyAPI.Wallet.GetNonFungibleTokens(walletData.Id).AwaitResult();
Console.WriteLine($"{nftTokens.Length} tokens(s) [ERC1155/ERC721] found for wallet with id={walletData.Id}\n");
}
//Retrieve Contracts (NFT API)
//******************
var contracts = await VenlyAPI.Nft.GetContracts().AwaitResult();
Console.WriteLine($"{contracts.Length} contract(s) retrieved\n");
if (!contracts.Any()) return;
//Retrieve TokenTypes (NFT API | Client)
//*******************
var sourceContract = contracts[0];
var tokenTypes = await VenlyAPI.Nft.GetTokenTypes(sourceContract.Id).AwaitResult();
Console.WriteLine($"{tokenTypes.Length} TokenTypes found for Contract with id={sourceContract.Id}");
if (!tokenTypes.Any()) return;
//Mint Token
//**********
var sourceTokenType = tokenTypes[0];
var sourceAddress = wallets[0].Address;
var mintingParams = new VyMintTokensDto()
{
Chain = wallets[0].Chain,
ContractAddress = sourceContract.Address,
TokenTypeId = sourceTokenType.Id,
Destinations = new []
{
new VyTokenDestinationDto()
{
Address = sourceAddress,
Amount = 1
}
}
};
var mintingResult = await VenlyAPI.Nft.MintTokens(mintingParams);
if (mintingResult.Success)
{
var mintInfo = mintingResult.Mints[0];
Console.WriteLine($"Token Minted! (tokenId={mintInfo.TokenId}, destination={mintInfo.Destination})");
}
else throw mintingResult.Exception;
//Others
//VenlyAPI.Nft.CreateContract(...)
//VenlyAPI.Nft.CreateTokenType(...)
//VenlyAPI.Wallet.TransferNonFungibleToken(...)
//...
Updated 10 days ago