You can use Endpoints - Create - REST API (Azure DevOps Service Endpoint) to create a service connection with "Python Package Download" type in Azure DevOps.
Here is an example of calling the REST API using PowerShell:
# Convert PAT to a Base64 string.
$pat = "{Personal Access Token}"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
# Set request headers.
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $base64AuthInfo))
$headers.Add("Content-Type", "application/json-patch+json")
$uri = "POST https://dev.azure.com/{organization}/_apis/serviceendpoint/endpoints?api-version=7.0"
# Set request body and convert it to JSON type.
$body = @(
@{
"name": "MyNewServiceConnectionName",
"type": "PythonPackageDownload",
"url": "https://myserver",
"authorization": @{
"parameters": @{
"username": "myusername",
"password": "mysecretpassword"
},
"scheme": "UsernamePassword"
},
"isShared": false,
"isReady": true,
"serviceEndpointProjectReferences": [
@{
"projectReference": @{
"id": "ProjectId",
"name": "ProjectName"
},
"name": "MyNewServiceConnectionName"
}
]
}
) | ConvertTo-Json -Depth 100
# Execute Invoke-RestMethod cmdlet to call the API and convert it response to JSON for better readable.
Invoke-RestMethod -Method POST -Uri $uri -Headers $headers -Body $body | ConvertTo-Json -Depth 100