Estimated reading time: 4 minutes

Call Vertex AI endpoint

To call your endpoint using HTTP, you’ll need to construct a POST request with the correct authorization and data format. Here’s a breakdown and an example using curl:

1. Prerequisites

  • Endpoint ID: You’ll need the ID of your Vertex AI endpoint. You can find this in the Google Console or by using the Vertex AI SDK (as shown in the previous response).
  • Google Cloud Credentials: You’ll need credentials to authorize the request. The easiest way to do this from your local machine is to have the Google Cloud SDK (gcloud) installed and configured.
  • Project ID and Region: You will need your Google Cloud Project ID and the region where you deployed the endpoint.

2. Authorization

Vertex AI requests require an authorization header with a valid access token. If you have the Google Cloud SDK installed, you can obtain an access token using the following command:

gcloud auth print-access-token

3. Construct the HTTP Request

You’ll make a POST request to the Vertex AI API endpoint. The URL will look like this:

https://{region}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{region}/endpoints/{endpoint_id}:predict
  • {project_id}: Your Google Cloud Project ID.
  • {region}: The region where your endpoint is deployed (e.g., “us-central1”).
  • {endpoint_id}: The ID of your Vertex AI endpoint.

The request body should be a object with an “instances” key. The value of “instances” is a list of data instances. In your case, each instance represents the features of a house for which you want to predict the price.

4. Example using curl

Here’s an example of how to call your endpoint using curl:

ACCESS_TOKEN=$(gcloud auth print-access-token)
PROJECT_ID="your-project-id"  # Replace with your Project ID
REGION="us-central1"      # Replace with your Region
ENDPOINT_ID="your-endpoint-id"  # Replace with your Endpoint ID

# Sample data (same as in the Python SDK example)
DATA='{
    "instances": [
        {
            "Size_LivingArea_SqFt": 2000,
            "Size_Lot_SqFt": 8000,
            "Size_TotalArea_SqFt": 2800,
            "Rooms_Total": 7,
            "Bedrooms": 3,
            "Bathrooms_Full": 2,
            "Bathrooms_Half": 1,
            "Basement_Area_SqFt": 800,
            "Basement_Finished": 1,
            "Garage_Cars": 2,
            "Fireplaces": 1,
            "Porch_Area_SqFt": 100,
            "Year_Built": 2000,
            "Year_Remodeled": 2010,
            "Condition_Overall": 7,
            "Quality_Overall": 7,
            "Building_Type": "House",
            "House_Style": "Ranch",
            "Foundation_Type": "Slab",
            "Roof_Material": "Composition Shingle",
            "Exterior_Material": "Brick",
            "Heating_Type": "Forced Air",
            "Cooling_Type": "Central AC",
            "Kitchen_Quality": "Good",
            "Bathroom_Quality": "Good",
            "Fireplace_Quality": "Average",
            "Basement_Quality": "Average",
            "Stories": 1,
            "Floor_Material": "Hardwood",
            "Neighborhood": "Bentonville Central",
            "Proximity_Schools_Miles": 0.5,
            "Proximity_Parks_Miles": 1.2,
            "Proximity_PublicTransport_Miles": 0.8,
            "Proximity_Shopping_Miles": 1.5,
            "Proximity_Hospitals_Miles": 2.0,
            "Safety_CrimeRate_Index": 65,
            "Environmental_NoiseLevel_dB": 45,
            "Environmental_AirQuality_Index": 35,
            "Flood_Zone": "No",
            "View": "None",
            "Time_of_Sale": "2024-08",
            "Interest_Rate": 6.2,
            "Inflation_Rate": 3.5,
            "Unemployment_Rate": 4.2,
            "Housing_Inventory": 0.05,
            "Economic_Growth_Rate": 2.5
        }
    ]
}'

# Construct the URL
URL="https://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${REGION}/endpoints/${ENDPOINT_ID}:predict"

# Make the POST request
curl -X POST \
     -H "Authorization: Bearer ${ACCESS_TOKEN}" \
     -H "Content-Type: application/json" \
     -d "${DATA}" \
     "${URL}"

Explanation:

  • ACCESS_TOKEN=$(gcloud auth print-access-token): Gets your current access token.
  • PROJECT_ID, REGION, ENDPOINT_ID: Replace these with your actual values.
  • DATA: A JSON string containing the input data. Crucially, it’s wrapped in an “instances” list.
  • URL: The Vertex AI API endpoint URL.
  • The curl command:
    • -X POST: Specifies the POST request method.
    • -H "Authorization: Bearer ${ACCESS_TOKEN}": Adds the authorization header.
    • -H "Content-Type: application/json": Sets the content type to JSON.
    • -d "${DATA}": Sends the JSON data in the request body.
    • ${URL}: The URL to send the request to.

5. Response

The response from the Vertex AI endpoint will be a JSON object with a “predictions” key. The value of “predictions” will be a list, where each element corresponds to the prediction for an instance in your input. In this case, you’ll get a list with a single element: the predicted house price.

Agentic AI (45) AI Agent (35) airflow (6) Algorithm (35) Algorithms (88) apache (57) apex (5) API (135) Automation (67) Autonomous (60) auto scaling (5) AWS (73) aws bedrock (1) Azure (47) BigQuery (22) bigtable (2) blockchain (3) Career (7) Chatbot (23) cloud (143) cosmosdb (3) cpu (45) cuda (14) Cybersecurity (19) database (138) Databricks (25) Data structure (22) Design (113) dynamodb (10) ELK (2) embeddings (39) emr (3) flink (12) gcp (28) Generative AI (28) gpu (25) graph (49) graph database (15) graphql (4) image (50) indexing (33) interview (7) java (43) json (79) Kafka (31) LLM (59) LLMs (55) Mcp (6) monitoring (128) Monolith (6) mulesoft (4) N8n (9) Networking (16) NLU (5) node.js (16) Nodejs (6) nosql (29) Optimization (91) performance (193) Platform (121) Platforms (96) postgres (5) productivity (31) programming (54) pseudo code (1) python (110) pytorch (22) Q&A (2) RAG (65) rasa (5) rdbms (7) ReactJS (1) realtime (2) redis (16) Restful (6) rust (3) salesforce (15) Spark (39) sql (70) tensor (11) time series (17) tips (14) tricks (29) use cases (93) vector (60) vector db (9) Vertex AI (23) Workflow (67)