Use your model with the prediction API

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


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

Publish your trained iteration

From the Custom Vision web page, select your project and then select the Performance tab.

To submit images to the Prediction API, you will first need to publish your iteration for prediction, which can be done by selecting Publish and specifying a name for the published iteration. This will make your model accessible to the Prediction API of your Custom Vision Azure resource.

The performance tab is shown, with a red rectangle surrounding the Publish button.

Once your model has been successfully published, you'll see a "Published" label appear next to your iteration in the left-hand sidebar, and its name will appear in the description of the iteration.

The performance tab is shown, with a red rectangle surrounding the Published label and the name of the published iteration.

Get the URL and prediction key

Once your model has been published, you can retrieve the required information by selecting Prediction URL. This will open up a dialog with information for using the Prediction API, including the Prediction URL and Prediction-Key.

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

The performance tab is shown with a red rectangle surrounding the Prediction URL value for using an image file and the Prediction-Key value.

In this guide, you will use a local image, so copy the URL under If you have an image file to a temporary location. Copy the corresponding Prediction-Key value as well.

Create the application

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

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

    using System;
    using System.IO;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;
    namespace CVSPredictionSample
        public static class Program
            public static void Main()
                Console.Write("Enter image file path: ");
                string imageFilePath = Console.ReadLine();
                Console.WriteLine("\n\nHit ENTER to exit...");
            public static async Task MakePredictionRequest(string imageFilePath)
                var client = new HttpClient();
                // Request headers - replace this example key with your valid Prediction-Key.
                client.DefaultRequestHeaders.Add("Prediction-Key", "<Your prediction key>");
                // Prediction URL - replace this example URL with your valid Prediction URL.
                string url = "<Your prediction 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());
            private 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);
  3. Change the following information:

    • Set the namespace field to the name of your project.
    • Replace the placeholder <Your prediction key> with the key value you retrieved earlier.
    • Replace the placeholder <Your prediction URL> with the URL you retrieved earlier.

Run the application

When you run the application, you are prompted to enter a path to an image file in the console. The image is then submitted to the Prediction API, and the prediction results are returned as a JSON-formatted string. The following is an example response.

        {"tagId":"d9cb3fa5-1ff3-4e98-8d47-2ef42d7fb373","tagName":"cat", "probability":1.0},
        {"tagId":"9a8d63fb-b6ed-4462-bcff-77ff72084d99","tagName":"dog", "probability":0.1087869}

Next steps

In this guide, you learned how to submit images to your custom image classifier/detector and receive a response programmatically with the C# SDK. Next, learn how to complete end-to-end scenarios with C#, or get started using a different language SDK.