Hello
We have a community build instance that we would like to know how many LOC in total we are using, in order to know which license we should buy on the Enterpise.
We have few hundreds of projects so doing the calculation manually is not an option.
michha
(Michael)
July 3, 2026, 2:19pm
2
In the Developer edition you can browse on /admin/license/app and see the LoC metric. Maybe this works on the Community edition too.
andres
(Andrés García Solares)
July 3, 2026, 3:10pm
3
Hey @Jonathan_Vila_Lopez , nice to see you here! It’s not the first time this has been asked , and it seems to me that the answer remains the same: you can either script against GET api/measures/component or you can use the sonar-loc tool that we provide as an unofficial utility, but which implements the functionality that you would anyways end up scripting yourself. You can get it from PyPI .
@michha unfortunately /admin/license/app won’t work on Community Build. After all, there’s no license, so there are no Admin > License settings!
michha
(Michael)
July 3, 2026, 3:52pm
4
Ok, good to know.
We also have a powershell script that lists every projects LoC and sums it
[string] $creds = 'Basic your-BASE64-encoded-access-token'
[string] $SQHOST = 'YOUR HOST'
[string] $url = "https://$SQHOST/api/projects/search?ps=500" # Page Size 500
$result = Invoke-WebRequest -Uri $url -UseBasicParsing -Headers @{'Authorization' = $creds }
$content = ConvertFrom-Json $result.Content
$projects = $content.components
[int[]] $locs = @()
[int] $totalLoc = 0
foreach ($prj in $projects) {
$url = "https://$SQHOST/api/measures/search_history?metrics=ncloc&component=$($prj.key)"
$result = Invoke-WebRequest -Uri $url -UseBasicParsing -Headers @{'Authorization' = $creds }
$content = ConvertFrom-Json $result.Content
$measures = $content.measures
if ($measures.Count -ne 1) {
Write-Error 'Unknown condition'
}
[int] $loc = 0
if ($measures[0].history.Count -ne 0) {
$datapoint = $measures[0].history[$measures[0].history.Count - 1]
if ($datapoint.PSObject.Properties.name -contains 'value') {
$loc = $datapoint.value
}
}
'{0, -20}: {1,7:N0} lines {2}' -f $prj.key, $loc, $datapoint.date
$locs += $loc
$totalLoc += $loc
}
$measure = $locs | Measure-Object -Sum -Average
'Total: {0:N0} lines in {1:N0} projects (avg {2:N0} lines)' -f $measure.Sum, $measure.Count, $measure.Average