How to create user with aliases via cli?

Your question

I have this script, that works but not the part with the aliases.
how can i make it work with that too?

#!/usr/bin/env bash
set -euo pipefail

CLI=“stalwart-cli”
URL=“https://domain.de
API_KEY=“API_3ZQ”

echo “== Lade Domains ==”

DOMAINS_OUTPUT=$(
$CLI --url “$URL”
–api-key “$API_KEY”
query Domain
)

echo “”
echo “Verfügbare Domains:”
echo “--------------------”
echo “$DOMAINS_OUTPUT”
echo “”

read -rp "Account Name (name): " NAME
read -rp "Full Name: " FULL_NAME
read -rp "Domain ID: " DOMAIN_ID
read -rp "Aliase (kommagetrennt, leer = keine): " ALIASES_INPUT

ALIASES_JSON=“

if [[ -n “${ALIASES_INPUT:-}” ]]; then
IFS=‘,’ read -ra ALIASES_ARRAY <<< “$ALIASES_INPUT”

ALIASES_JSON=“[”
first=true

for alias in “${ALIASES_ARRAY[@]}”; do
alias=$(echo “$alias” | xargs)
[[ -z “$alias” ]] && continue

if [[ "$first" == true ]]; then
  first=false
else
  ALIASES_JSON+=","
fi

ALIASES_JSON+="{\"localPart\":\"$alias\",\"domainId\":\"$DOMAIN_ID\",\"enabled\":true}"

done

ALIASES_JSON+=“]”
fi

echo “== Erstelle Account ==”

$CLI --url “$URL”
–api-key “$API_KEY”
create Account/User
–field “name=$NAME”
–field “description=$FULL_NAME”
–field “domainId=$DOMAIN_ID”
–field ‘credentials={“0”:{“@type”:“Password”,“secret”:“AnfangsPasswort”}}’
–field ‘memberGroupIds={}’
–field ‘roles={“@type”:“User”}’
–field ‘permissions={“@type”:“Inherit”}’
–field ‘locale=de_DE’
–field ‘timeZone=Europe/Berlin’
–field ‘quotas={“maxDiskQuota”:524288000}’
–field “aliases={}”
–field ‘encryptionAtRest={“@type”:“Disabled”}’

$CLI --url “$URL”
–api-key “$API_KEY”
update Account/User
–field “name=$NAME”
–field “aliases=$ALIASES_JSON”
root@netcup-server1:/opt/stalwart#

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

Found the issue. The EmailAlias struct uses the field name for the local part, not localPart. Your JSON is using "localPart" which is incorrect - it must be "name".

Change your alias JSON construction to use name instead of localPart:

ALIASES_JSON+="{\"name\":\"$alias\",\"domainId\":\"$DOMAIN_ID\",\"enabled\":true}"

So the full corrected loop would be:

for alias in "${ALIASES_ARRAY[@]}"; do
  alias=$(echo "$alias" | xargs)
  [[ -z "$alias" ]] && continue

  if [[ "$first" == true ]]; then
    first=false
  else
    ALIASES_JSON+=","
  fi

  ALIASES_JSON+="{\"name\":\"$alias\",\"domainId\":\"$DOMAIN_ID\",\"enabled\":true}"
done

Also note: your update command is missing the account ID. The update subcommand requires the object ID to know which account to update. You should capture the ID returned from the create call, or query the account ID by name first and pass it as the second positional argument:

ACCOUNT_ID=$(stalwart-cli ... query Account --filter "name=$NAME" ... )

stalwart-cli --url "$URL" --api-key "$API_KEY" \
  update Account "$ACCOUNT_ID" \
  --field "aliases=$ALIASES_JSON"

See the Account object reference for the full EmailAlias field definitions.

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

here’s my complete script, the solution to the problem: