Skip to content
Start here

Create routing rule

client.EmailRouting.Rules.New(ctx, params) (*EmailRoutingRule, error)
POST/zones/{zone_id}/email/routing/rules

Rules consist of a set of criteria for matching emails (such as an email being sent to a specific custom email address) plus a set of actions to take on the email (like forwarding it to a specific destination address).

Security

API Email + API Key

The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.

Example:X-Auth-Email: user@example.com

The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.

Example:X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194
Accepted Permissions (at least one required)
Email Routing Rules Write
ParametersExpand Collapse
params RuleNewParams
ZoneID param.Field[string]

Path param: Identifier.

maxLength32
Actions param.Field[[]Action]

Body param: List actions patterns.

Type ActionType

Type of supported action.

One of the following:
const ActionTypeDrop ActionType = "drop"
const ActionTypeForward ActionType = "forward"
const ActionTypeWorker ActionType = "worker"
Value []stringoptional
Matchers param.Field[[]Matcher]

Body param: Matching patterns to forward to your actions.

Type MatcherType

Type of matcher.

One of the following:
const MatcherTypeAll MatcherType = "all"
const MatcherTypeLiteral MatcherType = "literal"
Field MatcherFieldoptional

Field for type matcher.

Value stringoptional

Value for matcher.

maxLength90
Enabled param.Field[RuleNewParamsEnabled]optional

Body param: Routing rule status.

const RuleNewParamsEnabledTrue RuleNewParamsEnabled = true
const RuleNewParamsEnabledFalse RuleNewParamsEnabled = false
Name param.Field[string]optional

Body param: Routing rule name.

maxLength256
Priority param.Field[float64]optional

Body param: Priority of the routing rule.

minimum0
ReturnsExpand Collapse
type EmailRoutingRule struct{…}
ID stringoptional

Routing rule identifier.

maxLength32
Actions []Actionoptional

List actions patterns.

Type ActionType

Type of supported action.

One of the following:
const ActionTypeDrop ActionType = "drop"
const ActionTypeForward ActionType = "forward"
const ActionTypeWorker ActionType = "worker"
Value []stringoptional
Enabled EmailRoutingRuleEnabledoptional

Routing rule status.

One of the following:
const EmailRoutingRuleEnabledTrue EmailRoutingRuleEnabled = true
const EmailRoutingRuleEnabledFalse EmailRoutingRuleEnabled = false
Matchers []Matcheroptional

Matching patterns to forward to your actions.

Type MatcherType

Type of matcher.

One of the following:
const MatcherTypeAll MatcherType = "all"
const MatcherTypeLiteral MatcherType = "literal"
Field MatcherFieldoptional

Field for type matcher.

Value stringoptional

Value for matcher.

maxLength90
Name stringoptional

Routing rule name.

maxLength256
Priority float64optional

Priority of the routing rule.

minimum0
DeprecatedTag stringoptional

Routing rule tag. (Deprecated, replaced by routing rule identifier)

maxLength32

Create routing rule

package main

import (
  "context"
  "fmt"

  "github.com/cloudflare/cloudflare-go"
  "github.com/cloudflare/cloudflare-go/email_routing"
  "github.com/cloudflare/cloudflare-go/option"
)

func main() {
  client := cloudflare.NewClient(
    option.WithAPIKey("144c9defac04969c7bfad8efaa8ea194"),
    option.WithAPIEmail("user@example.com"),
  )
  emailRoutingRule, err := client.EmailRouting.Rules.New(context.TODO(), email_routing.RuleNewParams{
    ZoneID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
    Actions: cloudflare.F([]email_routing.ActionParam{email_routing.ActionParam{
      Type: cloudflare.F(email_routing.ActionTypeForward),
    }}),
    Matchers: cloudflare.F([]email_routing.MatcherParam{email_routing.MatcherParam{
      Type: cloudflare.F(email_routing.MatcherTypeLiteral),
    }}),
  })
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", emailRoutingRule.ID)
}
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "success": true,
  "result": {
    "id": "a7e6fb77503c41d8a7f3113c6918f10c",
    "actions": [
      {
        "type": "forward",
        "value": [
          "destinationaddress@example.net"
        ]
      }
    ],
    "enabled": true,
    "matchers": [
      {
        "type": "literal",
        "field": "to",
        "value": "test@example.com"
      }
    ],
    "name": "Send to user@example.net rule.",
    "priority": 0,
    "tag": "a7e6fb77503c41d8a7f3113c6918f10c"
  }
}
Returns Examples
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "success": true,
  "result": {
    "id": "a7e6fb77503c41d8a7f3113c6918f10c",
    "actions": [
      {
        "type": "forward",
        "value": [
          "destinationaddress@example.net"
        ]
      }
    ],
    "enabled": true,
    "matchers": [
      {
        "type": "literal",
        "field": "to",
        "value": "test@example.com"
      }
    ],
    "name": "Send to user@example.net rule.",
    "priority": 0,
    "tag": "a7e6fb77503c41d8a7f3113c6918f10c"
  }
}