> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wistx.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Get Cost Analysis

> Get aggregated cost analysis for an indexed repository

> Get aggregated cost analysis from knowledge articles for an indexed repository

<CodeGroup>
  ```bash cURL theme={null}
  curl --request GET \
    --url 'https://api.wistx.ai/v1/indexing/resources/res_abc123/cost-analysis?refresh=false' \
    --header 'Authorization: Bearer YOUR_API_KEY'
  ```

  ```python Python theme={null}
  import requests

  api_key = "YOUR_API_KEY"
  resource_id = "res_abc123"

  response = requests.get(
      f"https://api.wistx.ai/v1/indexing/resources/{resource_id}/cost-analysis",
      headers={"Authorization": f"Bearer {api_key}"},
      params={"refresh": False}
  )

  analysis = response.json()
  print(f"Cost analysis for: {analysis['repository_url']}")
  ```

  ```javascript JavaScript theme={null}
  const apiKey = "YOUR_API_KEY";
  const resourceId = "res_abc123";

  const response = await fetch(
    `https://api.wistx.ai/v1/indexing/resources/${resourceId}/cost-analysis?refresh=false`,
    {
      headers: {
        Authorization: `Bearer ${apiKey}`
      }
    }
  );

  const analysis = await response.json();
  console.log(`Cost analysis for: ${analysis.repository_url}`);
  ```

  ```go Go theme={null}
  package main

  import (
      "encoding/json"
      "fmt"
      "net/http"
      "net/url"
  )

  func main() {
      apiKey := "YOUR_API_KEY"
      resourceID := "res_abc123"
      baseURL := fmt.Sprintf("https://api.wistx.ai/v1/indexing/resources/%s/cost-analysis", resourceID)
      
      params := url.Values{}
      params.Add("refresh", "false")
      fullURL := baseURL + "?" + params.Encode()

      req, _ := http.NewRequest("GET", fullURL, nil)
      req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))

      client := &http.Client{}
      resp, _ := client.Do(req)
      defer resp.Body.Close()

      var analysis map[string]interface{}
      json.NewDecoder(resp.Body).Decode(&analysis)
      fmt.Printf("Cost analysis for: %v\n", analysis["repository_url"])
  }
  ```

  ```ruby Ruby theme={null}
  require 'net/http'
  require 'json'
  require 'uri'

  api_key = "YOUR_API_KEY"
  resource_id = "res_abc123"
  url = URI("https://api.wistx.ai/v1/indexing/resources/#{resource_id}/cost-analysis?refresh=false")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true

  request = Net::HTTP::Get.new(url)
  request["Authorization"] = "Bearer #{api_key}"

  response = http.request(request)
  analysis = JSON.parse(response.body)
  puts "Cost analysis for: #{analysis['repository_url']}"
  ```

  ```php PHP theme={null}
  <?php

  $apiKey = "YOUR_API_KEY";
  $resourceId = "res_abc123";
  $url = "https://api.wistx.ai/v1/indexing/resources/" . $resourceId . "/cost-analysis?refresh=false";

  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, [
      "Authorization: Bearer " . $apiKey
  ]);

  $response = curl_exec($ch);
  curl_close($ch);

  $analysis = json_decode($response, true);
  echo "Cost analysis for: " . $analysis['repository_url'];
  ```

  ```java Java theme={null}
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import com.google.gson.Gson;
  import com.google.gson.JsonObject;

  public class GetCostAnalysis {
      public static void main(String[] args) throws Exception {
          String apiKey = "YOUR_API_KEY";
          String resourceId = "res_abc123";
          URL url = new URL("https://api.wistx.ai/v1/indexing/resources/" + resourceId + "/cost-analysis?refresh=false");
          
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setRequestMethod("GET");
          conn.setRequestProperty("Authorization", "Bearer " + apiKey);
          
          BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
          StringBuilder response = new StringBuilder();
          String responseLine;
          while ((responseLine = br.readLine()) != null) {
              response.append(responseLine.trim());
          }
          
          Gson gson = new Gson();
          JsonObject analysis = gson.fromJson(response.toString(), JsonObject.class);
          System.out.println("Cost analysis for: " + analysis.get("repository_url"));
      }
  }
  ```
</CodeGroup>

## Path Parameters

<ParamField path="resource_id" type="string" required>
  Resource ID of the indexed repository.
</ParamField>

## Query Parameters

<ParamField query="refresh" type="boolean">
  Force recalculation of cost analysis.

  **Default:** `false`
</ParamField>

## Response

<ResponseExample>
  ```json 200 Success theme={null}
  {
    "resource_id": "res_abc123",
    "repository_url": "https://github.com/owner/repo",
    "environment_name": "production",
    "cost_analysis": {
      "total_monthly_cost_usd": 1250.50,
      "by_service": {
        "EC2": 800.00,
        "RDS": 350.00,
        "S3": 100.50
      },
      "by_resource_type": {
        "t3.large": 400.00,
        "db.t3.medium": 350.00
      },
      "optimization_opportunities": [
        {
          "service": "EC2",
          "current_cost": 400.00,
          "optimized_cost": 250.00,
          "savings": 150.00,
          "recommendation": "Use Reserved Instances"
        }
      ]
    }
  }
  ```

  ```json 401 Unauthorized theme={null}
  {
    "detail": "User ID not found"
  }
  ```

  ```json 404 Not Found theme={null}
  {
    "detail": "Resource not found or access denied"
  }
  ```

  ```json 500 Internal Server Error theme={null}
  {
    "detail": "Failed to get cost analysis"
  }
  ```
</ResponseExample>

## Related Endpoints

* [Get Compliance Analysis](/api-reference/indexing/compliance-analysis) - Get compliance analysis
* [Get Resource Status](/api-reference/indexing/resource-status) - Check resource status
* [Cost Search](/api-reference/cost-search) - Search cost data


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.