Use the prediction endpoint to test images programmatically with a Custom Vision Service classifier

After you train your model, you can test images programmatically by submitting them to the Prediction API.


This document demonstrates using C# to submit an image to the Prediction API. For more information and examples of using the API, see the Prediction API reference.

Get the URL and prediction key

From the Custom Vision web page, select your project and then select the Performance tab. To display information about using the Prediction API, including the Prediction-key, select Prediction URL. For projects attached to an Azure Resource, your Prediction-key can also be found in the Azure Portal page for associated Azure Resource under Keys. Copy the following information for use in the application:

  • URL for using an image file.
  • Prediction-key value.


If you have multiple iterations, you can control which one is used by setting it as default. Select the iteration from the Iterations section, then select Make default at the top of the page.

The performance tab is shown with a red rectangle surrounding the Prediction URL.

Create the application

  1. From Visual Studio, create a new C# console application.

  2. Use the following code as the body of the Program.cs file.


    Change the following information:

    • Set the namespace to the name of your project.
    • Set the Prediction-Key value you received earlier in the line that begins with client.DefaultRequestHeaders.Add("Prediction-Key",.
    • Set the URL value you received earlier in the line that begins with string url =.
    using System;
    using System.IO;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;
    namespace CSPredictionSample
        static class Program
            static void Main()
                Console.Write("Enter image file path: ");
                string imageFilePath = Console.ReadLine();
                Console.WriteLine("\n\n\nHit ENTER to exit...");
            static byte[] GetImageAsByteArray(string imageFilePath)
                FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
                BinaryReader binaryReader = new BinaryReader(fileStream);
                return binaryReader.ReadBytes((int)fileStream.Length);
            static async Task MakePredictionRequest(string imageFilePath)
                var client = new HttpClient();
                // Request headers - replace this example key with your valid subscription key.
                client.DefaultRequestHeaders.Add("Prediction-Key", "13hc77781f7e4b19b5fcdd72a8df7156");
                // Prediction URL - replace this example URL with your valid prediction URL.
                string url = "";
                HttpResponseMessage response;
                // Request body. Try this sample with a locally stored image.
                byte[] byteData = GetImageAsByteArray(imageFilePath);
                using (var content = new ByteArrayContent(byteData))
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    response = await client.PostAsync(url, content);
                    Console.WriteLine(await response.Content.ReadAsStringAsync());

Use the application

When running the application, you enter the path to an image file. The image is submitted to the API and the results are returned as a JSON document. The following JSON is an example of the response


Next steps

Export the model for mobile use