We now have an early version of a native .NET client for calling algorithms and interacting with our Data APIs. This guide will give you a walkthrough of how to use the new .NET client. The client is open-sourced and available on GitHub.
The Algorithmia .NET client supports .NET Standard 2.0 which means it can be used within apps targeting:
- .NET Core
- ASP.NET Core
- .NET Framework (> 4.6.1)
- Universal Windows Platform Apps
Getting Started with Algorithmia in .NET
The Algorithmia client is available on NuGet.org and is as easy as adding the package to your .NET project using Visual Studio or the NuGet Packet Manager.
Calling your first algorithm
Calling an algorithm is extremely simple with the .NET Client. You first create a client using your API key. You can find your API key at https://algorithmia.com/user#credentials.
Now, you can find any algorithm in the directory at https://algorithmia.com/algorithms and call it. In this case, we are going to call the Hello demo algorithm.
A single algorithm may have different input and output types, or accept multiple types of input, so consult the algorithm’s description for usage examples specific to that algorithm.
Your account can make up to 80 Algorithmia requests at the same time (this limit can be raised if needed).
Create a Data Collection
This section will show how to create a data collection which is essentially a folder of data files hosted on Algorithmia for free.
Start with adding the required import and creating the client as we did before.
Now create a data collection called nlp_directory:
A Data URI uniquely identifies files and directories and contains a protocol “data://” and path “YOUR_USERNAME/data_collection”. For more information on the Data URI see the Data API Specification.
Instead of your username you can also use ‘.my’ when calling algorithms. For more information about the ‘.my’ pseudonym check out the Hosted Data Guide.
Work with Directory Permissions
When we created the data collection in the previous code snippet, the default setting is
ReadDataAcl.MY_ALGOS which is a permission type that allows other users on the platform to interact with your data through the algorithms you create if you decide to contribute to algorithm development. This means users can call your algorithm to perform an operation on your data stored in this collection, otherwise the algorithm you created would only work for you.
In order to inspect the data collection’s permission type and update those permissions to private:
Notice that we changed our data collection to private, which means that only you will be able to read and write to your data collection.
Note that read access that is set to the default
DataMyAlgorithms allows any algorithm you call to have access to your data collection so most often, this is the setting you want when you are calling an algorithm and are an application developer.
For more information on collection-based Access Control Lists (ACLs) and other data collection permissions go to the Hosted Data Guide.
Upload Data to your Data Collection
So far you’ve created your data collection and checked and updated directory permissions. Now you’re ready to upload a local text file to your data collection using the Data API.
First add the necessary dependencies:
Then create a variable that holds the local path to the file you will be uploading and a DataFile object that represents the destination in the nlp_directory data collection we created earlier:
Next upload your local file using the
This method will replace a file if it already exists. If you wish to avoid replacing a file, check if the file exists before using this.
You can confirm that the file was created by navigating to Algorithmia’s Hosted Data Source and finding your data collection and file.
Downloading Data from a Data Collection
Next check if the file that you just uploaded to data collections exists, and try downloading it to a (new) local file:
This copies the file from your data collection and saves it as a file on your local machine, storing the filename in the variable
Alternately, if you just need the text content of the file to be stored in a variable, you can retrieve the remote file’s content without saving the actual file:
This will get your file as a string, saving it to the variable
input. If the file was binary (an image, etc), you could instead use the function
.getBytes() to retrieve the file’s content as a byte array. For more image-manipulation tutorials, see the Computer Vision Recipes.
Now you’ve seen how to upload a local data file, check if a file exists in a data collection, and download the file contents as a string.
For more methods on how to get a file using the Data API from a data collection go to the API Specification.