> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mailchannels.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Create Sub-account

> Creates a new sub-account under the parent account.
Each sub-account must have a unique handle composed solely of lowercase alphanumeric characters.
If no handle is provided, a random handle will be generated.




## OpenAPI

````yaml /email-api/api-reference/openapi.yaml post /sub-account
openapi: 3.0.0
info:
  contact:
    email: support@mailchannels.com
    name: MailChannels Corporation
  description: >
    This is the API reference for Email API. Here, you'll find more detailed
    information on every endpoint, including parameters, response formats, and
    error codes.
  title: Email API
  version: 1.6.0
servers:
  - url: https://api.mailchannels.net/tx/v1
security: []
tags:
  - name: Send
  - name: DKIM
  - name: Metrics
  - name: Sub-accounts
  - name: Suppression
  - name: Webhooks
  - name: Usage
  - name: Custom Tracking
paths:
  /sub-account:
    post:
      tags:
        - Sub-accounts
      summary: Create Sub-account
      description: >
        Creates a new sub-account under the parent account.

        Each sub-account must have a unique handle composed solely of lowercase
        alphanumeric characters.

        If no handle is provided, a random handle will be generated.
      parameters:
        - in: header
          name: X-Api-Key
          required: true
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SubAccountData'
        description: The details of the sub-account to create.
        required: false
      responses:
        '201':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SubAccountDetails'
          description: The sub-account was successfully created.
        '400':
          description: Malformed request. The request body is invalid.
        '403':
          description: >-
            The operation is forbidden. The parent account does not have
            permission to create sub-accounts.
        '409':
          description: A sub-account with the specified name already exists.
        '500':
          description: An unexpected internal error occurred.
      x-codeSamples:
        - label: Node.js
          lang: javascript
          source: >-
            import { MailChannels } from "mailchannels-sdk";


            const { PARENT_API_KEY, COMPANY_NAME, SUB_ACCOUNT_HANDLE } =
            process.env;

            if (!PARENT_API_KEY) throw new Error("Set PARENT_API_KEY (a
            parent-account API key) before running");

            if (!COMPANY_NAME) throw new Error("Set COMPANY_NAME (display name
            for the sub-account)");


            const mailchannels = new MailChannels(PARENT_API_KEY);


            // SUB_ACCOUNT_HANDLE is optional. If omitted, MailChannels
            generates a random handle.

            const { data, error } = await
            mailchannels.subAccounts.create(COMPANY_NAME, SUB_ACCOUNT_HANDLE);


            if (error) {
              console.error("Create sub-account failed:", error);
              process.exit(1);
            }

            console.log(data);
        - label: Python
          lang: python
          source: >-
            import os

            import sys


            import mailchannels


            parent_api_key = os.environ.get("PARENT_API_KEY")

            company_name = os.environ.get("COMPANY_NAME")

            if not parent_api_key:
                sys.exit("Set PARENT_API_KEY (a parent-account API key) before running")
            if not company_name:
                sys.exit("Set COMPANY_NAME (display name for the sub-account)")

            mailchannels.api_key = parent_api_key


            # SUB_ACCOUNT_HANDLE is optional. If omitted, MailChannels generates
            a random handle.

            response = mailchannels.SubAccounts.create(
                company_name=company_name,
                handle=os.environ.get("SUB_ACCOUNT_HANDLE"),
            )


            print(response)
        - label: PHP
          lang: php
          source: >-
            <?php


            require_once __DIR__ . '/vendor/autoload.php';


            use MailChannels\Client;


            $parent_api_key = getenv('PARENT_API_KEY') or exit("Error:
            PARENT_API_KEY is not set\n");

            $company_name   = getenv('COMPANY_NAME') or exit("Error:
            COMPANY_NAME is not set\n");


            $client = new Client(apiKey: $parent_api_key);


            // SUB_ACCOUNT_HANDLE is optional. If omitted, MailChannels
            generates a random handle.

            $response = $client->subAccounts->create(
                companyName: $company_name,
                handle:      getenv('SUB_ACCOUNT_HANDLE') ?: null,
            );


            print_r($response->toArray());
components:
  schemas:
    SubAccountData:
      properties:
        company_name:
          description: >
            The name of the company associated with the sub-account.

            This name is used for display purposes only and does not affect the
            functionality of the sub-account.

            The length must be between 3 and 128 characters.
          type: string
        handle:
          description: >
            A unique name for the sub-account to be created.

            The length must be between 3 and 128 characters, and it may contain
            only lowercase letters and numbers.

            If not provided, a random handle will be generated.
          type: string
      required:
        - company_name
      type: object
    SubAccountDetails:
      properties:
        company_name:
          type: string
        enabled:
          type: boolean
        handle:
          type: string
      required:
        - handle
        - enabled
      type: object

````