Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time

Quickstart: Analyze a remote image using the Computer Vision REST API with Go

In this quickstart, you'll analyze a remotely stored image to extract visual features using the Computer Vision REST API. With the Analyze Image method, you can extract visual features based on image content.


  • An Azure subscription - Create one for free
  • Go
  • Once you have your Azure subscription, create a Computer Vision resource in the Azure portal to get your key and endpoint. After it deploys, click Go to resource.
    • You will need the key and endpoint from the resource you create to connect your application to the Computer Vision service. You'll paste your key and endpoint into the code below later in the quickstart.
    • You can use the free pricing tier (F0) to try the service, and upgrade later to a paid tier for production.
  • Create environment variables for the key and endpoint URL, named COMPUTER_VISION_SUBSCRIPTION_KEY and COMPUTER_VISION_ENDPOINT, respectively.

Create and run the sample

To create and run the sample, do the following steps:

  1. Copy the below code into a text editor.
  2. Optionally, replace the value of imageUrl with the URL of a different image that you want to analyze.
  3. Save the code as a file with a .go extension. For example, analyze-image.go.
  4. Open a command prompt window.
  5. At the prompt, run the go build command to compile the package from the file. For example, go build analyze-image.go.
  6. At the prompt, run the compiled package. For example, analyze-image.
package main

import (

func main() {
    // Add your Computer Vision subscription key and endpoint to your environment variables.
    subscriptionKey := os.Getenv("COMPUTER_VISION_SUBSCRIPTION_KEY")
    endpoint := os.Getenv("COMPUTER_VISION_ENDPOINT")

    uriBase := endpoint + "vision/v3.1/analyze"
    const imageUrl =

    const params = "?visualFeatures=Description&details=Landmarks"
    uri := uriBase + params
    const imageUrlEnc = "{\"url\":\"" + imageUrl + "\"}"

    reader := strings.NewReader(imageUrlEnc)

    // Create the HTTP client
    client := &http.Client{
        Timeout: time.Second * 2,

    // Create the POST request, passing the image URL in the request body
    req, err := http.NewRequest("POST", uri, reader)
    if err != nil {

    // Add request headers
    req.Header.Add("Content-Type", "application/json")
    req.Header.Add("Ocp-Apim-Subscription-Key", subscriptionKey)

    // Send the request and retrieve the response
    resp, err := client.Do(req)
    if err != nil {

    defer resp.Body.Close()

    // Read the response body
    // Note, data is a byte array
    data, err := ioutil.ReadAll(resp.Body)
    if err != nil {

    // Parse the JSON data from the byte array
    var f interface{}
    json.Unmarshal(data, &f)

    // Format and display the JSON result
    jsonFormatted, _ := json.MarshalIndent(f, "", "  ")

Examine the response

A successful response is returned in JSON. The sample application parses and displays a successful response in the command prompt window, similar to the following example:

  "categories": [
      "detail": {
        "landmarks": []
      "name": "outdoor_water",
      "score": 0.9921875
  "description": {
    "captions": [
        "confidence": 0.916458423253597,
        "text": "a large waterfall over a rocky cliff"
    "tags": [
  "metadata": {
    "format": "Jpeg",
    "height": 959,
    "width": 1280
  "requestId": "a92f89ab-51f8-4735-a58d-507da2213fc2"

Next steps

Explore the Computer Vision API used to analyze an image, detect celebrities and landmarks, create a thumbnail, and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API testing console.