> ## 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 DKIM Key Pair

> Create a DKIM key pair for a specified domain and selector using the specified algorithm and key length,
for the current customer.




## OpenAPI

````yaml /email-api/api-reference/openapi.yaml post /domains/{domain}/dkim-keys
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.4.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:
  /domains/{domain}/dkim-keys:
    post:
      tags:
        - DKIM
      summary: Create DKIM Key Pair
      description: >
        Create a DKIM key pair for a specified domain and selector using the
        specified algorithm and key length,

        for the current customer.
      parameters:
        - in: path
          name: domain
          required: true
          schema:
            type: string
        - in: header
          name: X-Api-Key
          required: true
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/DKIMKeyPairCreateRequest'
        required: true
      responses:
        '201':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/DKIMKeyInfo'
          description: Key pair created successfully
        '400':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
          description: Bad Request
        '409':
          description: |
            Key pair already created for domain, and selector
        '500':
          description: Internal Server Error
      x-codeSamples:
        - label: Node.js
          lang: javascript
          source: >-
            import { MailChannels } from "mailchannels-sdk";


            const { MAILCHANNELS_API_KEY, DOMAIN, SELECTOR } = process.env;

            if (!MAILCHANNELS_API_KEY) throw new Error("Set MAILCHANNELS_API_KEY
            before running");

            if (!DOMAIN) throw new Error("Set DOMAIN (the sending domain, e.g.
            example.com)");

            if (!SELECTOR) throw new Error("Set SELECTOR (e.g. mc1)");


            const mailchannels = new MailChannels(MAILCHANNELS_API_KEY);


            const { data, error } = await
            mailchannels.domains.dkim.create(DOMAIN, {
              selector: SELECTOR,
              algorithm: "rsa",
              length: 2048,
            });


            if (error) {
              console.error("Create DKIM key failed:", error);
              process.exit(1);
            }

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

            import sys


            import mailchannels


            if not os.environ.get("MAILCHANNELS_API_KEY"):
                sys.exit("Set MAILCHANNELS_API_KEY before running")

            domain = os.environ.get("DOMAIN")

            selector = os.environ.get("SELECTOR")

            if not domain:
                sys.exit("Set DOMAIN (the sending domain, e.g. example.com)")
            if not selector:
                sys.exit("Set SELECTOR (e.g. mc1)")

            # Create a MailChannels-managed DKIM key pair. The response includes
            the

            # public key and the DNS TXT record to publish.

            response = mailchannels.Dkim.create(
                domain,
                selector=selector,
                algorithm="rsa",
                key_length=2048,
            )


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


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


            use MailChannels\Client;


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

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

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


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


            // Create a MailChannels-managed DKIM key pair. The response
            includes the

            // public key and the DNS TXT record to publish.

            $response = $client->dkim->create(
                domain:    $domain,
                selector:  $selector,
                algorithm: 'rsa',
                keyLength: 2048,
            );


            print_r($response->toArray());
components:
  schemas:
    DKIMKeyPairCreateRequest:
      properties:
        algorithm:
          default: rsa
          description: |
            Algorithm used for the new key pair
            Currently, only RSA is supported.
          enum:
            - rsa
          type: string
        key_length:
          default: 2048
          description: |
            Key length in bits.
            For RSA, must be a multiple of 1024. Common values: 1024 or 2048.
            Defaults to 2048 bits.
          maximum: 4096
          minimum: 1024
          type: integer
        selector:
          description: |
            Selector for the new key pair
          maxLength: 63
          minLength: 1
          type: string
      required:
        - selector
      type: object
    DKIMKeyInfo:
      properties:
        algorithm:
          description: |
            Algorithm used for the key pair
          type: string
        created_at:
          description: |
            Timestamp when the key pair was created
          format: date-time
          nullable: true
          type: string
        dkim_dns_records:
          description: |
            Suggested DNS records for the DKIM key
          items:
            $ref: '#/components/schemas/DKIMDnsRecord'
          type: array
        domain:
          description: |
            Domain associated with the key pair
          type: string
        gracePeriodExpiresAt:
          description: >
            UTC timestamp after which you can no longer use the rotated key for
            signing
          format: date-time
          nullable: true
          type: string
        key_length:
          description: |
            Key length in bits
          type: integer
        public_key:
          type: string
        retiresAt:
          description: |
            UTC timestamp when a rotated key pair is retired
          format: date-time
          nullable: true
          type: string
        selector:
          description: |
            Selector assigned to the key pair
          type: string
        status:
          enum:
            - active
            - retired
            - revoked
            - rotated
          type: string
        status_modified_at:
          description: |
            Timestamp when the key was last modified
          format: date-time
          nullable: true
          type: string
      required:
        - domain
        - selector
        - public_key
        - status
        - algorithm
      type: object
    ErrorResponse:
      properties:
        errors:
          description: >
            This is an array error objects that provide machine and human
            readable descriptions of the errors.
          items:
            type: string
          type: array
      type: object
    DKIMDnsRecord:
      description: |
        Suggested DNS record for the DKIM key
      nullable: true
      properties:
        name:
          type: string
        type:
          example: TXT
          type: string
        value:
          type: string
      required:
        - name
        - type
        - value
      type: object

````