PinataClient is a class for interacting with the Pinata API, allowing you to upload, pin, retrieve, update, and delete files on IPFS (InterPlanetary File System). The client requires a JWT (JSON Web Token) for authorization.
To use the PinataClient, ensure you have the following NuGet packages installed:
- Newtonsoft.Json
- HeyRed.Mime
To create an instance of PinataClient, you need to provide a JWT:
var pinataClient = new PinataClient("your_jwt_here");public async Task<PinFileResponse> PinFileToIPFSAsync(string filePath)Pins a file to IPFS.
filePath(string): The path of the file to be pinned.
PinFileResponse: A response containing the details of the pinned file.
- Throws
JsonExceptionif the file pinning fails.
var pinFileResponse = await pinataClient.PinFileToIPFSAsync("path/to/your/file.txt");public async Task<FileResponse> UploadFileAsync(Stream fileStream, string fileName, string groupId = "")Uploads a file to Pinata.
fileStream(Stream): The stream of the file to be uploaded.fileName(string): The name of the file (including extension).groupId(string, optional): An optional group ID for the file.
FileResponse: A response containing the upload details.
- Throws
JsonExceptionif the upload fails.
using (var fileStream = File.OpenRead("path/to/your/file.txt"))
{
var fileResponse = await pinataClient.UploadFileAsync(fileStream, "file.txt");
}public async Task<GetFilesListResponse> GetFilesListAsync()Retrieves a list of files from Pinata.
GetFilesListResponse: A response containing a list of files.
- Throws
Exceptionif the retrieval fails.
var filesListResponse = await pinataClient.GetFilesListAsync();public async Task<FileResponse> GetFileById(string id)Gets a file by its ID.
id(string): The ID of the file to retrieve.
FileResponse: A response containing the file details.
- Throws
Exceptionif the retrieval fails.
var fileResponse = await pinataClient.GetFileById("your_file_id");public async Task<FileResponse> UpdateFileNameAsync(string id, string newName)Updates the name of a specified file.
id(string): The ID of the file to update.newName(string): The new name for the file.
FileResponse: A response containing the updated file details.
- Throws
Exceptionif the update fails.
var updatedFileResponse = await pinataClient.UpdateFileNameAsync("your_file_id", "new_file_name.txt");public async Task<FileResponse> DeleteFileAsync(string id)Deletes a specified file.
id(string): The ID of the file to delete.
FileResponse: A response confirming the deletion of the file.
- Throws
Exceptionif the deletion fails.
var deleteFileResponse = await pinataClient.DeleteFileAsync("your_file_id");The PinataClient methods throw exceptions on failure, allowing you to handle errors gracefully in your application. Ensure that you wrap calls in try-catch blocks as needed.
try
{
var fileResponse = await pinataClient.UploadFileAsync(fileStream, "file.txt");
}
catch (JsonException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
}The PinataClient class provides a simple interface for interacting with the Pinata API, enabling file management on IPFS. Use the methods provided to easily pin, upload, retrieve, update, and delete files.