# Automating Account and Wallet Creation

Automating the creation of accounts and wallets in the ledger system is essential for efficiently onboarding new clients, setting up operational structures, and scaling financial workflows. This guide provides a detailed tutorial on automating account and wallet creation, complete with reusable code samples for integrating client applications with the ledger via **Kafka** and **HTTPS**.

***

### **Why Automate Account and Wallet Creation?**

#### **1. Improved Efficiency**

Eliminate manual processes by automatically creating accounts and wallets for customers, business units, or operational needs.

#### **2. Scalability**

Support large-scale account onboarding or operational setups by automating repetitive tasks.

#### **3. Accuracy**

Reduce errors and ensure consistency by standardizing the creation process.

#### **4. Integration Readiness**

Seamlessly connect client applications with the ledger, enabling real-time or batched creation workflows.

***

### **Setting Up Automation**

#### **Integration Options**

1. **Kafka**: Use for event-driven, asynchronous creation of accounts and wallets.
2. **HTTPS**: Use for synchronous, REST-based creation workflows.

***

### **Automated Account and Wallet Creation via Kafka**

#### **1. Producing Account and Wallet Creation Events**

Use Kafka producers to send account and wallet creation commands as events.

**Reusable Code Sample (Python):**

```python
from kafka import KafkaProducer
import json

producer = KafkaProducer(
    bootstrap_servers='kafka-broker:9092',
    value_serializer=lambda v: json.dumps(v).encode('utf-8')
)

# Example: Creating an account and wallet
account_event = {
    "operation": "create_account",
    "account_type": "group",
    "account_reference": "customer123",
    "wallets": [
        {
            "wallet_name": "default",
            "currency": "USD",
            "accounting_type": "LIABILITY",
            "decimal_places": 2
        }
    ],
    "metadata": {
        "customer_id": "customer123",
        "description": "Customer 123 primary account"
    }
}

producer.send('ledger.account.creation', value=account_event)
print("Account creation event sent successfully!")
```

***

#### **2. Consuming Account and Wallet Creation Responses**

Listen for ledger responses to confirm account and wallet creation success or handle errors.

**Reusable Code Sample (Python):**

```python
from kafka import KafkaConsumer
import json

consumer = KafkaConsumer(
    'ledger.account.responses',
    bootstrap_servers='kafka-broker:9092',
    value_deserializer=lambda m: json.loads(m.decode('utf-8'))
)

for message in consumer:
    print(f"Ledger response received: {message.value}")
```

***

### **Automated Account and Wallet Creation via HTTPS**

#### **1. Creating Accounts**

Use the `/api/v1/account/` endpoint to create a new account.

**Reusable Code Sample (Python):**

```python
import requests

url = "https://ledger.example.com/api/v1/account/"
headers = {
    "Authorization": "Bearer <API_KEY>",
    "Content-Type": "application/json"
}

account_payload = {
    "account_type": "group",
    "account_reference": "customer123",
    "metadata": {
        "customer_id": "customer123",
        "description": "Customer 123 primary account"
    }
}

response = requests.post(url, json=account_payload, headers=headers)

if response.status_code == 201:
    print("Account created successfully!")
else:
    print(f"Error: {response.status_code}, {response.json()}")
```

***

#### **2. Adding Wallets to Accounts**

Use the `/api/v1/wallet/` endpoint to create wallets under an account.

**Reusable Code Sample (Python):**

```python
url = "https://ledger.example.com/api/v1/wallet/"
wallet_payload = {
    "account_reference": "customer123",
    "wallet_name": "default",
    "currency": "USD",
    "accounting_type": "LIABILITY",
    "decimal_places": 2
}

response = requests.post(url, json=wallet_payload, headers=headers)

if response.status_code == 201:
    print("Wallet created successfully!")
else:
    print(f"Error: {response.status_code}, {response.json()}")
```

***

### **Use Cases for Automated Account and Wallet Creation**

#### **1. Customer Onboarding**

**Scenario**: A business needs to onboard new customers with unique accounts and wallets.\
**Integration Example (Kafka)**:

```python
account_event = {
    "operation": "create_account",
    "account_type": "group",
    "account_reference": "customer456",
    "wallets": [
        {
            "wallet_name": "default",
            "currency": "EUR",
            "accounting_type": "LIABILITY",
            "decimal_places": 2
        }
    ]
}
```

***

#### **2. Multi-Currency Support**

**Scenario**: Automatically create wallets for multiple currencies under a single account.\
**Integration Example (HTTPS)**:

```python
wallet_payload = [
    {
        "account_reference": "customer123",
        "wallet_name": "USD Wallet",
        "currency": "USD",
        "accounting_type": "LIABILITY",
        "decimal_places": 2
    },
    {
        "account_reference": "customer123",
        "wallet_name": "EUR Wallet",
        "currency": "EUR",
        "accounting_type": "LIABILITY",
        "decimal_places": 2
    }
]
```

***

#### **3. Operational Account Setup**

**Scenario**: Create accounts and wallets for internal operational purposes, such as payroll or revenue management.\
**Integration Example (HTTPS)**:

```python
account_payload = {
    "account_type": "chart",
    "account_reference": "company_revenue",
    "metadata": {
        "description": "Company Revenue Account"
    }
}

wallet_payload = {
    "account_reference": "company_revenue",
    "wallet_name": "default",
    "currency": "USD",
    "accounting_type": "ASSET",
    "decimal_places": 2
}
```

***

### **Best Practices for Automated Account and Wallet Creation**

1. **Standardize Naming Conventions**:
   * Use consistent account and wallet names for better organization.
2. **Leverage Metadata**:
   * Add meaningful metadata for reporting and tracking.
3. **Monitor Responses**:
   * Ensure every creation event is confirmed by checking Kafka or HTTPS responses.
4. **Retry Failed Operations**:
   * Implement retry mechanisms for transient failures, such as network issues.
5. **Audit Account and Wallet Creation**:
   * Use logs and monitoring tools to track all creation events for compliance.

***

Automating account and wallet creation streamlines client onboarding, supports multi-currency operations, and ensures scalability. By leveraging Kafka for real-time, event-driven workflows or HTTPS for synchronous, REST-based operations, businesses can seamlessly integrate their applications with the ledger system.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.xyb.co/xyb-ledger/integrate-with-xyb-ledger/set-up-transaction-workflows/automating-account-and-wallet-creation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
