Forum Discussion

tbutcher's avatar
tbutcher
New Contributor
29 days ago

Is it possible to create a new item in a vault using an Azure function app without Connect Server?

I need to know if creating a new item using rest api in an azure powershell function app is possible. Here is a snippet of what I've been trying:

# Get 1Password API token and vault ID from environment variables
$apiToken = $env:1PASSWORD_API_TOKEN
$vaultId = $env:1PASSWORD_VAULT_ID

# Set up the API URL
$apiUrl = "https://api.1password.com/v1/vaults/$vaultId/items"

# Prepare the request body with 'apikey' as the literal value
$body = @{
    title    = "APIM Credentials - $customer"
    category = "API_CREDENTIAL"
    fields   = @(
        @{ label = "username"; value = "apikey"; type = "STRING" },  # Use the literal 'apikey' as the value
        @{ label = "password"; value = $primaryKey; type = "CONCEALED" }
    )
} | ConvertTo-Json -Depth 10

# Set headers
$headers = @{
    "Authorization" = "Bearer $apiToken"
    "Content-Type"  = "application/json"
}

# Send request to create item
try {
    $response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $body
    Write-Host "Created API key in 1Password: $($response.id)"
} catch {
    Write-Host "ERROR: Failed to create item in 1Password"
    Write-Host "Response: $($_.Exception.Message)"
    return
}

3 Replies