netbox 0.3.3

ergonomic rust client for NetBox 4.x REST API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# client library

this crate provides a typed, ergonomic client for the netbox 4.x rest api.

## features

- **typed resources** - strongly-typed request/response models for all netbox endpoints
- **crud operations** - list, get, create, update, patch, delete with query filtering
- **pagination** - iterator-based pagination with automatic page fetching
- **bulk operations** - batch create, update, patch, and delete
- **specialized endpoints** - ipam availability, dcim cable tracing, config rendering, task management
- **graphql** - read-only graphql query support
- **error handling** - structured errors with status codes and api messages

## modules

the client exposes netbox endpoints through typed module accessors:

- `client.dcim()` - devices, interfaces, sites, racks, cables, power
- `client.ipam()` - prefixes, ip addresses, vlans, vrfs, asns
- `client.circuits()` - providers, circuits, terminations
- `client.virtualization()` - clusters, virtual machines, vm interfaces
- `client.tenancy()` - tenants, contacts
- `client.extras()` - tags, custom fields, config contexts, webhooks
- `client.core()` - background tasks, data sources, jobs
- `client.users()` - users, groups, tokens, permissions
- `client.vpn()` - tunnels, ike/ipsec policies, l2vpns
- `client.wireless()` - wireless lans, links
- `client.plugins()` - netbox-branching plugin support

each module returns typed `Resource<T>` handles with standard crud methods, plus specialized action methods where applicable.

## resource pattern

all netbox endpoints follow the same pattern. access a module, then a resource, then call an action:

```text
client.{module}().{resource}().{action}()
```

every `Resource<T>` provides these standard methods:

| method | http | description |
|--------|------|-------------|
| `list(query)` | GET | fetch paginated results with optional filters |
| `get(id)` | GET | fetch a single item by id |
| `create(body)` | POST | create a new item |
| `update(id, body)` | PUT | full replacement update |
| `patch(id, body)` | PATCH | partial update |
| `delete(id)` | DELETE | remove an item |
| `bulk_create(items)` | POST | create multiple items |
| `bulk_update(items)` | PUT | full update multiple items |
| `bulk_patch(items)` | PATCH | partial update multiple items |
| `bulk_delete(items)` | DELETE | remove multiple items |
| `paginate(query)` | GET | returns an iterator for all pages |

## query filtering

use `QueryBuilder` to filter, sort, and limit results:

```text
QueryBuilder::new()
    .filter("key", "value")     // add a filter
    .filter("status", "active") // filters are additive
    .search("term")             // full-text search
    .order_by("name")           // sort ascending
    .order_by("-created")       // sort descending (prefix with -)
    .limit(50)                  // max results per page
    .offset(100)                // skip first N results
```

pass the builder to `list()` or `paginate()` to apply filters.

## install

```toml
[dependencies]
netbox = "0.3"
tokio = { version = "1.0", features = ["full"] }
```

optional tracing instrumentation:

```toml
[dependencies]
netbox = { version = "0.3", features = ["tracing"] }
tokio = { version = "1.0", features = ["full"] }
tracing-subscriber = "0.3"
```

tiny runtime setup example:

```rust,ignore
use netbox::{Client, ClientConfig};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt()
        .with_env_filter("netbox=trace")
        .init();

    let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
    let _ = client.status().status().await?;
    Ok(())
}
```

## create a client

```rust,no_run
use netbox::{Client, ClientConfig};

fn example() -> Result<(), Box<dyn std::error::Error>> {
    let config = ClientConfig::new("https://netbox.example.com", "token");
    let _client = Client::new(config)?;
    Ok(())
}
```

## auth and config

```rust,no_run
use std::time::Duration;
use netbox::ClientConfig;
use reqwest::header::{HeaderName, HeaderValue};

fn example() {
    let _config = ClientConfig::new("https://netbox.example.com", "token")
        .with_timeout(Duration::from_secs(60))
        .with_max_retries(5)
        .with_ssl_verification(false)
        .with_header(
            HeaderName::from_static("x-netbox-client"),
            HeaderValue::from_static("custom"),
        );
}
```

## http customization hooks

for cross-cutting behavior, you can inject a prebuilt `reqwest::Client`,
customize the internal client builder, and attach request/response hooks.

```rust,no_run
use netbox::{Client, ClientConfig, HttpHooks};
use reqwest::{Method, Request, StatusCode};
use std::time::Duration;

struct MetricsHook;

impl HttpHooks for MetricsHook {
    fn on_request(&self, _method: &Method, _path: &str, request: &mut Request) -> netbox::Result<()> {
        request
            .headers_mut()
            .insert("x-client-hook", "enabled".parse().expect("valid header value"));
        Ok(())
    }

    fn on_response(&self, method: &Method, path: &str, status: StatusCode, duration: Duration) {
        println!("{method} {path} -> {status} in {duration:?}");
    }
}

fn example() -> Result<(), Box<dyn std::error::Error>> {
    let prebuilt = reqwest::Client::builder()
        .pool_max_idle_per_host(4)
        .build()?;

    let config = ClientConfig::new("https://netbox.example.com", "token")
        .with_http_client(prebuilt)
        .with_http_hooks(MetricsHook);

    let _client = Client::new(config)?;
    Ok(())
}
```

precedence and behavior:
- `with_http_client(...)` takes precedence over `with_http_client_builder(...)`.
- hooks run for all client-driven HTTP requests (`Resource<T>`, special endpoint helpers, and `request_raw`).
- hooks are additive with tracing; they can be used independently or together.

safety notes:
- avoid logging or exporting authentication tokens, cookies, or full sensitive payloads from hooks.
- prefer additive mutations (headers/metadata) over replacing request method/url/body unexpectedly.
- keep hook logic lightweight; slow hooks directly add latency to every request.

## http client access

```rust,no_run
use netbox::{Client, ClientConfig};

fn example() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
    let http = client.http_client();
    let _ = http;
    Ok(())
}
```

## list and filter

```rust,no_run
use netbox::{Client, ClientConfig, QueryBuilder};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
let query = QueryBuilder::new()
    .filter("status", "active")
    .limit(50)
    .order_by("name");

let page = client.dcim().devices().list(Some(query)).await?;
println!("{}", page.count);
# Ok(())
# }
```

## paginate

```rust,no_run
use netbox::{Client, ClientConfig};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
let mut paginator = client.dcim().devices().paginate(None)?;
while let Some(page) = paginator.next_page().await? {
    for device in page.results {
        let display = device.display.as_deref().unwrap_or("<unknown>");
        println!("{display}");
    }
}
# Ok(())
# }
```

note: for local dev instances with self-signed certs, call `with_ssl_verification(false)` in your config.
note: `paginate` returns `Result<Paginator<T>>`. handle errors before calling `next_page`.

## dynamic resources

```rust,no_run
use netbox::{Client, ClientConfig};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
let resource = client.resource("ipam/vrfs/");
let page = resource.list(None).await?;
println!("{}", page.count);
# Ok(())
# }
```

## create, update, delete

```rust,no_run
use netbox::{Client, ClientConfig};
use netbox::dcim::CreateDeviceRequest;

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
let request = CreateDeviceRequest {
    name: "switch-01".to_string(),
    device_type: 1,
    role: 1,
    site: 1,
    status: Some("active".to_string()),
    serial: None,
    asset_tag: None,
    tags: None,
};

let device = client.dcim().devices().create(&request).await?;
let device_id = device.id.expect("device id missing from response") as u64;
let _updated = client
    .dcim()
    .devices()
    .patch(device_id, &serde_json::json!({"status": "offline"}))
    .await?;
client.dcim().devices().delete(device_id).await?;
# Ok(())
# }
```

## bulk operations

```rust,no_run
use netbox::{BulkDelete, BulkUpdate, Client, ClientConfig};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;

let updates = vec![
    BulkUpdate::new(1, serde_json::json!({"status": "offline"})),
    BulkUpdate::new(2, serde_json::json!({"status": "offline"})),
];
let _updated = client.dcim().devices().bulk_patch(&updates).await?;

let deletes = vec![BulkDelete::new(1), BulkDelete::new(2)];
client.dcim().devices().bulk_delete(&deletes).await?;
# Ok(())
# }
```

## status and schema

```rust,no_run
use netbox::{Client, ClientConfig};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
let status = client.status().status().await?;
let schema = client.schema().schema(Some("json"), None).await?;
# Ok(())
# }
```

## graphql (read-only)

returns the `data` field when present.

```rust,no_run
use netbox::{Client, ClientConfig};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
let data = client
    .graphql()
    .query("{ devices { name } }", None)
    .await?;
println!("{}", data);
# Ok(())
# }
```

## connected device

```rust,no_run
use netbox::{Client, ClientConfig};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
let devices = client
    .dcim()
    .connected_device("leaf-01", "Ethernet1")
    .await?;
println!("{}", devices.len());
# Ok(())
# }
```

## branching plugin

```rust,no_run
use netbox::{Client, ClientConfig};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
let branches = client.plugins().branches().list(None).await?;
println!("{}", branches.count);
# Ok(())
# }
```

## ipam availability

query and allocate available IPs, prefixes, VLANs, and ASNs.

```rust,no_run
use netbox::{Client, ClientConfig};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;

// list available IPs in a prefix
let available = client.ipam().available_ips_in_prefix(1).await?;
println!("{} IPs available", available.len());

// allocate IPs from a prefix
let requests = vec![serde_json::json!({"description": "allocated via api"})];
let created = client.ipam().create_available_ips_in_prefix(1, &requests).await?;
println!("created {} IPs", created.len());

// list available VLANs in a group
let vlans = client.ipam().available_vlans_in_group(1).await?;
println!("{} VLANs available", vlans.len());
# Ok(())
# }
```

## dcim trace

trace cable paths for interfaces and power components.

```rust,no_run
use netbox::{Client, ClientConfig};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
let trace = client.dcim().trace_interface(42).await?;
println!("{:?}", trace);
# Ok(())
# }
```

## core task management

manage background tasks and sync data sources.

```rust,no_run
use netbox::{Client, ClientConfig};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;

// sync a data source
let source = client.core().sync_data_source(1).await?;
println!("{:?}", source);

// manage background tasks
let task = client.core().enqueue_task("abc123").await?;
println!("{:?}", task);
# Ok(())
# }
```

## extras sync and render

sync config contexts/templates from data sources and render templates.

```rust,no_run
use netbox::{Client, ClientConfig};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;

// sync a config context from its data source
let context = client.extras().sync_config_context(1).await?;
println!("{:?}", context);

// render a config template
let output = client.extras().render_config_template(1).await?;
println!("{}", output);
# Ok(())
# }
```

## virtualization render config

render configuration for virtual machines.

```rust,no_run
use netbox::{Client, ClientConfig};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
let config = client.virtualization().render_vm_config(1).await?;
println!("{}", config);
# Ok(())
# }
```

## error handling

```rust,no_run
use netbox::{Client, ClientConfig, Error};

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
match client.dcim().devices().get(999u64).await {
    Ok(device) => println!("{}", device.display.as_deref().unwrap_or("<unknown>")),
    Err(Error::ApiError { status, .. }) if status == 404 => {
        println!("not found");
    }
    Err(e) if e.is_auth_error() => {
        println!("auth failed: {}", e);
    }
    Err(e) => {
        println!("error: {}", e);
    }
}
# Ok(())
# }
```

## raw api access

use this when you need an endpoint not yet wrapped by the high-level client.

```rust,no_run
use netbox::{Client, ClientConfig};
use netbox::openapi::apis::dcim_api;

# async fn example() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(ClientConfig::new("https://netbox.example.com", "token"))?;
let openapi_config = client.openapi_config()?;
let device = dcim_api::dcim_devices_retrieve(&openapi_config, 42).await?;
println!("{}", device.display.as_deref().unwrap_or("<unknown>"));
# Ok(())
# }
```

## coverage summary

high-level resource coverage exists for dcim, circuits, core, extras, ipam, tenancy, users, virtualization, vpn, and wireless. additional endpoints include status, schema, connected-device, graphql (read-only), and netbox-branching plugin resources.

action endpoints include ipam availability queries (prefixes, ip-ranges, vlan-groups, asn-ranges), dcim cable tracing, core task management and data source sync, extras config sync/render operations, and virtualization vm config rendering.