netbox-integration-best-practices

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

NetBox Integration Best Practices Skill

NetBox集成最佳实践指南

This skill provides best practices guidance for building integrations and automations with NetBox REST and GraphQL APIs.
本指南提供了使用NetBox REST与GraphQL API构建集成和自动化的最佳实践建议。

Target Audience

目标受众

  • Engineers building integrations atop NetBox APIs
  • Teams planning new automations with Claude
  • Developers learning NetBox API best practices
Scope: This skill covers API integration patterns. It does NOT cover plugin development, custom scripts, or NetBox administration.
  • 基于NetBox API构建集成的工程师
  • 计划使用Claude开发新自动化的团队
  • 学习NetBox API最佳实践的开发者
范围:本指南涵盖API集成模式。不包含插件开发、自定义脚本或NetBox管理相关内容。

NetBox Version Requirements

NetBox版本要求

FeatureVersion Required
REST APIAll versions
GraphQL API2.9+
v2 Tokens4.5+ (use these!)
v1 Token Deprecation4.7+ (migrate before this)
Primary target: NetBox 4.4+ with 4.5+ for v2 token features.
特性所需版本
REST API所有版本
GraphQL API2.9+
v2 Tokens4.5+(推荐使用!)
v1 Token 弃用4.7+(在此之前完成迁移)
主要目标版本:NetBox 4.4+,若需v2 token功能则需4.5+。

When to Apply This Skill

适用场景

Apply these practices when:
  • Building new NetBox API integrations
  • Reviewing existing integration code
  • Troubleshooting performance issues
  • Planning automation architecture
  • Writing scripts that interact with NetBox
在以下场景中应用这些实践:
  • 构建新的NetBox API集成
  • 审查现有集成代码
  • 排查性能问题
  • 规划自动化架构
  • 编写与NetBox交互的脚本

Priority Levels

优先级等级

LevelDescriptionAction
CRITICALSecurity vulnerabilities, data loss, severe performanceMust fix immediately
HIGHSignificant performance/reliability impactShould fix soon
MEDIUMNotable improvements, best practicesPlan to address
LOWMinor improvements, optionalConsider when convenient
等级描述行动
CRITICAL(严重)存在安全漏洞、数据丢失风险、严重性能问题必须立即修复
HIGH(高)对性能/可靠性有显著影响应尽快修复
MEDIUM(中)可带来明显改进,属于最佳实践计划解决
LOW(低)微小改进,可选方便时考虑处理

Quick Reference

快速参考

Authentication

身份验证

  • Use v2 tokens on NetBox 4.5+:
    Bearer nbt_<key>.<token>
  • Migrate from v1 before NetBox 4.7 (deprecation planned)
  • NetBox 4.5+使用v2 tokens
    Bearer nbt_<key>.<token>
  • 在NetBox 4.7前迁移v1 token(计划弃用)

REST API

REST API

  • Always paginate:
    ?limit=100
    (max 1000)
  • Use PATCH for partial updates, not PUT
  • Use ?brief=True for list operations
  • Exclude config_context:
    ?exclude=config_context
    (major performance impact)
  • Avoid ?q= search filter at scale; use specific filters
  • Bulk operations use list endpoints with JSON arrays (not separate endpoints)
  • 始终启用分页
    ?limit=100
    (最大1000)
  • 使用PATCH进行部分更新,而非PUT
  • 列表操作使用?brief=True
  • 排除config_context
    ?exclude=config_context
    (对性能影响极大)
  • 大规模场景避免使用?q=搜索过滤器;使用特定过滤器
  • 批量操作使用列表端点和JSON数组(而非独立端点)

GraphQL

GraphQL

  • Use the query optimizer: netbox-graphql-query-optimizer
  • Always paginate every list query
  • Paginate at every level of nesting
  • Beware offset pagination at scale: Deep offsets are slow; use ID range filtering in 4.5.x, cursor-based in 4.6.0+ (#21110)
  • Request only needed fields
  • Keep depth ≤3, never exceed 5
  • 使用查询优化器netbox-graphql-query-optimizer
  • 所有列表查询均需分页
  • 嵌套层级的每个列表都要分页
  • 注意大规模场景下的偏移分页:深层偏移速度缓慢;4.5.x版本使用ID范围过滤,4.6.0+版本使用基于游标分页(#21110
  • 仅请求所需字段
  • 查询深度≤3,绝不超过5

Performance

性能优化

  • Exclude config_context from device lists
  • Use brief mode for large lists
  • Parallelize independent requests
  • 设备列表中排除config_context
  • 大型列表使用精简模式(brief mode)
  • 并行处理独立请求

Data Ingestion (Diode)

数据导入(Diode)

  • For high-volume data ingestion, use Diode instead of direct API
  • Specify dependencies by name, not ID—Diode resolves or creates them
  • No dependency order needed—Diode handles object creation order
  • Use
    pip install netboxlabs-diode-sdk
    for Python
  • Use REST/GraphQL API for reading; use Diode for writing/populating
  • 高容量数据导入场景,使用Diode而非直接调用API
  • 按名称指定依赖项,而非ID——Diode会自动解析或创建依赖
  • 无需考虑依赖顺序——Diode会处理对象创建顺序
  • Python环境使用
    pip install netboxlabs-diode-sdk
    安装SDK
  • 读取数据使用REST/GraphQL API;写入/填充数据使用Diode

Branching (Plugin)

分支管理(插件)

Requires netbox-branching plugin.
  • Lifecycle: Create → Wait (PROVISIONING→READY) → Work → Sync → Merge
  • Context header:
    X-NetBox-Branch: {schema_id}
    (8-char ID, not name)
  • Async operations: sync/merge/revert return Job objects—poll for completion
  • Dry-run: All async ops accept
    {"commit": false}
    for validation
需要安装netbox-branching插件。
  • 生命周期:创建 → 等待(PROVISIONING→READY) → 工作 → 同步 → 合并
  • 上下文头
    X-NetBox-Branch: {schema_id}
    (8位ID,而非名称)
  • 异步操作:同步/合并/回滚会返回Job对象——轮询获取完成状态
  • 试运行:所有异步操作支持传入
    {"commit": false}
    进行验证

Rules by Category

分类规则

Authentication Rules

身份验证规则

RuleImpactDescription
auth-use-v2-tokensCRITICALUse v2 tokens on NetBox 4.5+
auth-provisioning-endpointMEDIUMUse provisioning endpoint for automated token creation
规则影响等级描述
auth-use-v2-tokensCRITICALNetBox 4.5+使用v2 tokens
auth-provisioning-endpointMEDIUM使用配置端点自动创建token

REST API Rules

REST API规则

RuleImpactDescription
rest-list-endpoint-bulk-opsCRITICALUse list endpoints for bulk operations
rest-pagination-requiredHIGHAlways paginate list requests
rest-patch-vs-putHIGHUse PATCH for partial updates
rest-brief-modeHIGHUse ?brief=True for lists
rest-field-selectionHIGHUse ?fields= to select fields
rest-exclude-config-contextHIGHExclude config_context from device lists
rest-avoid-search-filter-at-scaleHIGHAvoid q= with large datasets
rest-filtering-expressionsMEDIUMUse lookup expressions
rest-custom-field-filtersMEDIUMFilter by custom fields
rest-nested-serializersLOWUnderstand nested serializers
rest-ordering-resultsLOWUse ordering parameter
rest-options-discoveryLOWUse OPTIONS for discovery
规则影响等级描述
rest-list-endpoint-bulk-opsCRITICAL使用列表端点执行批量操作
rest-pagination-requiredHIGH列表请求必须分页
rest-patch-vs-putHIGH使用PATCH进行部分更新
rest-brief-modeHIGH列表使用?brief=True
rest-field-selectionHIGH使用?fields=选择字段
rest-exclude-config-contextHIGH设备列表排除config_context
rest-avoid-search-filter-at-scaleHIGH大规模数据集避免使用q=
rest-filtering-expressionsMEDIUM使用查找表达式
rest-custom-field-filtersMEDIUM按自定义字段过滤
rest-nested-serializersLOW了解嵌套序列化器
rest-ordering-resultsLOW使用排序参数
rest-options-discoveryLOW使用OPTIONS进行发现

GraphQL Rules

GraphQL规则

RuleImpactDescription
graphql-use-query-optimizerCRITICALUse query optimizer
graphql-always-paginateCRITICALPaginate every list query
graphql-pagination-at-each-levelHIGHPaginate nested lists
graphql-select-only-neededHIGHRequest only needed fields
graphql-calibrate-optimizerHIGHCalibrate against production
graphql-max-depthHIGHKeep depth ≤3
graphql-prefer-filtersMEDIUMFilter server-side
graphql-vs-rest-decisionMEDIUMChoose appropriate API
graphql-complexity-budgetsLOWEstablish complexity budgets
规则影响等级描述
graphql-use-query-optimizerCRITICAL使用查询优化器
graphql-always-paginateCRITICAL所有列表查询均需分页
graphql-pagination-at-each-levelHIGH嵌套列表分页
graphql-select-only-neededHIGH仅请求所需字段
graphql-calibrate-optimizerHIGH针对生产环境校准优化器
graphql-max-depthHIGH查询深度≤3
graphql-prefer-filtersMEDIUM服务端过滤
graphql-vs-rest-decisionMEDIUM选择合适的API
graphql-complexity-budgetsLOW设定复杂度预算

Performance Rules

性能规则

RuleImpactDescription
perf-exclude-config-contextHIGHExclude config_context
perf-brief-mode-listsHIGHUse brief mode for lists
规则影响等级描述
perf-exclude-config-contextHIGH排除config_context
perf-brief-mode-listsHIGH列表使用精简模式

Data Modeling Rules

数据建模规则

RuleImpactDescription
data-dependency-orderCRITICALCreate objects in dependency order
data-site-hierarchyMEDIUMUnderstand site hierarchy
data-ipam-hierarchyMEDIUMUnderstand IPAM hierarchy
data-custom-fieldsMEDIUMUse custom fields properly
data-tags-usageMEDIUMUse tags for classification
data-tenant-isolationMEDIUMUse tenants for separation
data-natural-keysMEDIUMUse natural keys
规则影响等级描述
data-dependency-orderCRITICAL按依赖顺序创建对象
data-site-hierarchyMEDIUM了解站点层级
data-ipam-hierarchyMEDIUM了解IPAM层级
data-custom-fieldsMEDIUM正确使用自定义字段
data-tags-usageMEDIUM使用标签进行分类
data-tenant-isolationMEDIUM使用租户进行隔离
data-natural-keysMEDIUM使用自然键

Integration Rules

集成规则

RuleImpactDescription
integ-diode-ingestionHIGHUse Diode for high-volume data ingestion
integ-pynetbox-clientHIGHUse pynetbox for Python
integ-branch-api-workflowHIGHComplete branching lifecycle (plugin)
integ-branch-context-headerHIGHBranch context with X-NetBox-Branch header (plugin)
integ-branch-async-operationsMEDIUMJob polling for sync/merge/revert (plugin)
integ-webhook-configurationMEDIUMConfigure webhooks
integ-change-trackingLOWQuery object changes
规则影响等级描述
integ-diode-ingestionHIGH高容量数据导入使用Diode
integ-pynetbox-clientHIGHPython环境使用pynetbox
integ-branch-api-workflowHIGH完整执行分支生命周期(插件)
integ-branch-context-headerHIGH使用X-NetBox-Branch头指定分支上下文(插件)
integ-branch-async-operationsMEDIUM同步/合并/回滚操作轮询Job状态(插件)
integ-webhook-configurationMEDIUM配置webhook
integ-change-trackingLOW查询对象变更

External References

外部参考

Official Documentation

官方文档

Essential Tools

必备工具

Community

社区资源

Reference Documentation

参考文档

DocumentPurpose
HUMAN.mdHuman-readable guide for engineers
netbox-integration-guidelines.mdComprehensive technical reference
文档用途
HUMAN.md面向工程师的可读指南
netbox-integration-guidelines.md综合技术参考