SKILL·8E456D

authorizing-api-requests

mailtrap
更新于 8 days ago
7
1
7
在 GitHub 上查看
aitestingapi

关于

This skill provides the definitive guide for authenticating all Mailtrap API requests, covering token selection, secure storage, and account_id resolution. Use it before implementing any API call to properly configure authorization headers and URL parameters. It serves as the central reference that other Mailtrap skills rely on for authentication patterns.

快速安装

Claude Code

推荐
主要方式
npx skills add mailtrap/mailtrap-skills -a claude-code
插件命令备选方式
/plugin add https://github.com/mailtrap/mailtrap-skills
Git 克隆备选方式
git clone https://github.com/mailtrap/mailtrap-skills.git ~/.claude/skills/authorizing-api-requests

在 Claude Code 中复制并粘贴此命令以安装该技能

技能文档

Authorizing Mailtrap API requests

Overview

Every Mailtrap API request needs two things:

  1. An API token in an auth header — proves identity and carries the scope.
  2. For account-scoped endpoints (most of them outside of the send hosts), an account_id in the URL path.

This skill is the single source of truth for both. Other skills (sending-emails, testing-with-sandbox, using-email-templates, managing-contacts, setting-up-sending-domain) reference these conventions instead of duplicating them.

When to use

  • Before writing any Mailtrap API call from code, scripts, CI, IaC, or an AI agent
  • Picking which token scope and stream to provision
  • Deciding where to store a token (env, secret manager, CI)
  • Resolving the account_id for an account-scoped endpoint
  • Debugging 401 Unauthorized / 403 Forbidden responses

API tokens

Create tokens at Settings > API Tokens with the smallest scope that works:

  • Email Sending API — for send.api.mailtrap.io and bulk.api.mailtrap.io. Scope per stream (transactional, bulk) when possible.
  • Email Testing API — for the Sandbox (sandbox.api.mailtrap.io). Always separate from live sending tokens.
  • Account-level API — for Contacts, Templates, Sending Domains, Suppressions, and other endpoints under https://mailtrap.io/api/accounts/{account_id}/....

A single token can cover several scopes if the user has the right plan; prefer narrower tokens (one stream / one project / one product surface) so a leak has limited blast radius. Reference: API tokens documentation.

Auth headers (two equivalent forms)

Mailtrap accepts either header. Use Bearer in examples — it's the more common HTTP convention and matches most generated SDK code.

FormHeaderWhen to use
Bearer (preferred)Authorization: Bearer $MAILTRAP_API_TOKENDefault for new code, SDKs, curl examples
Api-Token (legacy)Api-Token: $MAILTRAP_API_TOKENOlder clients or where Bearer is awkward

Do not send both at the same time. The same value goes in either header.

Where to put tokens

  • Local dev: environment variable, or .env file that is in .gitignore. Load with direnv, dotenv, or the framework's built-in mechanism.
  • CI / build: the CI provider's encrypted secret store (GitHub Actions secrets, GitLab CI variables, CircleCI contexts). Inject as env vars only.
  • Production / staging: a real secret manager (AWS Secrets Manager, GCP Secret Manager, Azure Key Vault, HashiCorp Vault, Doppler, 1Password, etc.). Rotate on a schedule.
  • Agent / LLM workflows: the host agent's secret store. Never paste a token into chat or a prompt.

Hard rules:

  • Never hardcode a token in source, config, or notebooks.
  • Never commit a token. If one lands in git, rotate it; history retention is forever.
  • Never pass a token on the command line as a flag — it leaks into shell history, ps, and CI logs.
  • Never let an LLM echo a literal token back into generated code. Use $VAR_NAME shell-var placeholders in all examples so generated code reaches for the env var, not the literal.
  • Never mix sandbox and live tokens. A leaked sandbox key must not be able to send real mail.

Recommended env var names

These names are used consistently across every other skill in this repo and across the example snippets below.

VariableUsed for
MAILTRAP_API_TOKENGeneral API: Email Send (transactional and bulk), Templates, Contacts, Sending Domains, Suppressions
MAILTRAP_SANDBOX_API_TOKENSandbox / Email Testing (separate scope)
MAILTRAP_ACCOUNT_IDPath parameter for account-scoped endpoints

If your environment uses different names, alias them once at startup so the examples in other skills work unchanged.

Resolving account_id automatically

account_id is the integer prefix on every https://mailtrap.io/api/accounts/{account_id}/... endpoint. Do not hardcode it. It changes between environments, is different per organization, and is silently wrong when you copy a script to a teammate's account.

Resolve it once per session from the Accounts endpoint, which lists every account the token can access:

curl -s https://mailtrap.io/api/accounts \
  -H "Authorization: Bearer $MAILTRAP_API_TOKEN"

Response shape (array):

[
  {"id": 12345, "name": "My Company", "access_levels": [1000]},
  {"id": 67890, "name": "Client Account", "access_levels": [100]}
]

access_levels values:

  • 1000 — Account owner
  • 100 — Admin
  • 10 — Viewer (read-only on most endpoints)

One-liner to cache as an env var (pick the right account if the token can see more than one):

export MAILTRAP_ACCOUNT_ID=$(curl -s https://mailtrap.io/api/accounts \
  -H "Authorization: Bearer $MAILTRAP_API_TOKEN" | jq '.[0].id')

Reference: Accounts API.

Quick reference

# Live sending (no account_id in path)
curl -X POST https://send.api.mailtrap.io/api/send \
  -H "Authorization: Bearer $MAILTRAP_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ ... }'

# Account-scoped endpoint
curl "https://mailtrap.io/api/accounts/$MAILTRAP_ACCOUNT_ID/contacts/lists" \
  -H "Authorization: Bearer $MAILTRAP_API_TOKEN"

# Sandbox / Testing
curl -X POST "https://sandbox.api.mailtrap.io/api/send/$MAILTRAP_INBOX_ID" \
  -H "Authorization: Bearer $MAILTRAP_SANDBOX_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ ... }'

Common mistakes

MistakeFix
Hardcoding the token in code, config, or a notebookLoad from $MAILTRAP_API_TOKEN (env, .env, CI secret, secret manager); rotate the token if it ever leaked
Passing the token as a CLI flag (--token=...)Use env vars; CLI flags leak to shell history, ps, and CI logs
Committing a token, then deleting it in a later commitHistory keeps the value forever — rotate the token immediately, do not just remove the file
Pasting a token into chat / prompt / issueTreat chat as public; rotate if it happened
Using the live MAILTRAP_API_TOKEN against the sandbox hostSandbox uses its own scope and MAILTRAP_SANDBOX_API_TOKEN; mixing them either fails or sends real mail by accident
Hardcoding account_idResolve via GET https://mailtrap.io/api/accounts once per run and pass through $MAILTRAP_ACCOUNT_ID
Picking the wrong account when the token can see severalFilter the GET /api/accounts response by name or access_levels (1000 = owner) instead of .[0]
Sending both Authorization and Api-Token headersPick one (Bearer for new code); duplicating them is unnecessary and confuses some intermediaries
Using a viewer-scoped token for writesCheck access_levels; writes need 100 (admin) or 1000 (owner) for the relevant account

GitHub 仓库

mailtrap/mailtrap-skills
路径: skills/authorizing-api-requests
0
FAQ

常见问题

什么是 authorizing-api-requests Skill?

authorizing-api-requests 是一个 Claude Skill,作者为 mailtrap。Skill 将 Claude 按需加载的说明和资源打包,让 Claude 无需额外提示即可执行与 authorizing-api-requests 相关的任务。

如何安装 authorizing-api-requests?

使用本页的安装命令:将 authorizing-api-requests 作为插件添加到 Claude Code,或将其仓库克隆到 skills 目录,然后重启 Claude 以加载该 Skill。

authorizing-api-requests 属于哪个分类?

authorizing-api-requests 属于元分类。

authorizing-api-requests 可以免费使用吗?

可以。authorizing-api-requests 已收录在 AIMCP,可免费安装。

相关推荐技能

content-collections

Content Collections 是一个 TypeScript 优先的构建工具,可将本地 Markdown/MDX 文件转换为类型安全的数据集合。它专为构建博客、文档站和内容密集型 Vite+React 应用而设计,提供基于 Zod 的自动模式验证。该工具涵盖从 Vite 插件配置、MDX 编译到生产环境部署的完整工作流。

查看技能
polymarket

这个Claude Skill为开发者提供完整的Polymarket预测市场开发支持,涵盖API调用、交易执行和市场数据分析。关键特性包括实时WebSocket数据流,可监控实时交易、订单和市场动态。开发者可用它构建预测市场应用、实施交易策略并集成实时市场预测功能。

查看技能
creating-opencode-plugins

该Skill帮助开发者创建OpenCode插件,用于接入命令、文件、LSP等25+种事件。它提供了插件结构、事件API规范和JavaScript/TypeScript实现模式,适合需要拦截操作、扩展功能或自定义事件处理的场景。开发者可通过它快速构建响应式模块来增强OpenCode AI助手的能力。

查看技能
sglang

SGLang是一个专为LLM设计的高性能推理框架,特别适用于需要结构化输出的场景。它通过RadixAttention前缀缓存技术,在处理JSON、正则表达式、工具调用等具有重复前缀的复杂工作流时,能实现极速生成。如果你正在构建智能体或多轮对话系统,并追求远超vLLM的推理性能,SGLang是理想选择。

查看技能