Use your Model with the Prediction API

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.

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 enable your model to be 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, as well as the name of the published iteration 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 information about using the Prediction API by selecting Prediction URL. This will open up a dialog like the one shown below 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.


Your Prediction-Key can also be found in the Azure Portal page for the Custom Vision Azure Resource associated to your project, under Keys.

From the dialog, copy the following information for use in the application:

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

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 retrieved earlier in the line that begins with client.DefaultRequestHeaders.Add("Prediction-Key",.
    • Set the Prediction URL value you retrieved 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 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", "3b9dde6d1ae1453a86bfeb1d945300f2");
                // 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());
            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);

Use the application

When running the application, you will enter the path to an image file in the console. The image is submitted to the Prediction API and the prediction results are returned as a JSON document. The following JSON is an example of the 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

Export the model for mobile use

Get started with .NET SDKs

Get started with Python SDKs

Get started with Java SDKs

Get started with Node SDKs

Get started with Go SDKs