Sign In

Developer Center

Resources to get you started with Algorithmia

Clients

.NET / C#

Updated
October 09, 2018

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.

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.

Install-PackageAlgorithmia.Client

Required imports

usingAlgorithmia;

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.

varclient=newClient("YOUR_API_KEY");

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.

varalgo=client.algo("algo://demo/hello");// The type you pass into the generic method `pipe` is the type you want to be returned as the result.
varresponse=algo.pipe<string>("World");vartextResult=response.result.ToString();

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.

Limits

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.

usingAlgorithmia;varclient=newClient("YOUR_API_KEY");

Now create a data collection called nlp_directory:

// Instantiate a DataDirectory object, set your data URI and call create
varnlp_directory=client.dir("data://YOUR_USERNAME/nlp_directory");// Create your data collection if it does not exist
if(!nlp_directory.exists()){nlp_directory.create();}

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:

usingSystem;usingSystem.IO;

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:

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:

// Download the file
vartext_file="data://YOUR_USERNAME/nlp_directory/jack_london.txt";if(client.file(text_file).exists()){varlocalfile=client.file(text_file).getFile();}else{Console.WriteLine("The file does not exist");}

This copies the file from your data collection and saves it as a file on your local machine, storing the filename in the variable localfile.

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:

// Download contents of file as a string
vartext_file="data://YOUR_USERNAME/nlp_directory/jack_london.txt";if(client.file(text_file).exists()){varinput=client.file(text_file).getString();}else{Console.WriteLine("The file does not exist");}

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.

Alternately, you can ensure that each of your servers interacting with your Algorithmia Enterprise instance have an environment variable named ALGORITHMIA_API and the client will use it. The fallback API endpoint is always the hosted Algorithmia marketplace service at https://api.algorithmia.com/

Additional information

You can find out more about the .NET Client from the GitHub repo. You can also find our API Specification available for all of the APIs that are available on the Algorithmia platform.