resend-rs 0.29.0

Resend's Official Rust SDK.
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
use std::sync::Arc;

use reqwest::Method;

use crate::{
    Config, Result,
    list_opts::{ListOptions, ListResponse},
    suppressions::types::SpecifiedMarker,
    types::{
        AddSuppressionOptions, AddSuppressionResponse, BatchAddSuppressionOptions,
        BatchAddSuppressionResponse, BatchRemoveSuppressionOptions,
        BatchRemoveSuppressionsResponse, RemoveSuppressionResponse, Suppression,
    },
};

/// `Resend` APIs for `/suppressions` endpoints.
#[derive(Clone, Debug)]
pub struct SuppressionsSvc(pub(crate) Arc<Config>);

impl SuppressionsSvc {
    /// Add an email address to the suppression list.
    ///
    /// <https://resend.com/docs/api-reference/suppressions/add-suppression>
    #[maybe_async::maybe_async]
    #[allow(clippy::needless_pass_by_value)]
    pub async fn add(&self, opts: AddSuppressionOptions) -> Result<AddSuppressionResponse> {
        let request = self.0.build(Method::POST, "/suppressions");
        let response = self.0.send(request.json(&opts)).await?;
        let content = response.json::<AddSuppressionResponse>().await?;

        Ok(content)
    }

    /// Retrieve a single suppression by ID or email.
    ///
    /// <https://resend.com/docs/api-reference/suppressions/get-suppression>
    #[maybe_async::maybe_async]
    pub async fn get(&self, id_or_email: &str) -> Result<Suppression> {
        let id_or_email = urlencoding::encode(id_or_email);
        let path = format!("/suppressions/{id_or_email}");

        let request = self.0.build(Method::GET, &path);
        let response = self.0.send(request).await?;
        let content = response.json::<Suppression>().await?;

        Ok(content)
    }

    /// Show all suppressions.
    ///
    /// - Default limit: 20
    ///
    /// <https://resend.com/docs/api-reference/suppressions/list-suppressions>
    #[maybe_async::maybe_async]
    #[allow(clippy::needless_pass_by_value)]
    pub async fn list<T>(&self, list_opts: ListOptions<T>) -> Result<ListResponse<Suppression>> {
        let request = self.0.build(Method::GET, "/suppressions").query(&list_opts);
        let response = self.0.send(request).await?;
        let content = response.json::<ListResponse<Suppression>>().await?;

        Ok(content)
    }

    /// Remove a single suppression by ID or email.
    ///
    /// <https://resend.com/docs/api-reference/suppressions/remove-suppression>
    #[maybe_async::maybe_async]
    pub async fn remove(&self, id_or_email: &str) -> Result<RemoveSuppressionResponse> {
        let id_or_email = urlencoding::encode(id_or_email);
        let path = format!("/suppressions/{id_or_email}");

        let request = self.0.build(Method::DELETE, &path);
        let response = self.0.send(request).await?;
        let content = response.json::<RemoveSuppressionResponse>().await?;

        Ok(content)
    }

    /// Add up to 100 email addresses to the suppression list at once.
    ///
    /// <https://resend.com/docs/api-reference/suppressions/add-suppressions>
    #[maybe_async::maybe_async]
    #[allow(clippy::needless_pass_by_value)]
    pub async fn batch_add(
        &self,
        opts: BatchAddSuppressionOptions,
    ) -> Result<BatchAddSuppressionResponse> {
        let request = self.0.build(Method::POST, "/suppressions/batch/add");
        let response = self.0.send(request.json(&opts)).await?;
        let content = response.json::<BatchAddSuppressionResponse>().await?;

        Ok(content)
    }

    /// Remove up to 100 suppressions from the suppression list at once.
    ///
    /// This endpoint requires that you have specified either ids or emails but not both,
    /// hence you need [`BatchRemoveSuppressionOptions<EmailsSpecified>`] or
    /// [`BatchRemoveSuppressionOptions<IdsSpecified>`].
    ///
    /// You can create those by doing:
    ///
    /// ```rust
    /// # use resend_rs::types::BatchRemoveSuppressionOptions;
    /// let _tmp = BatchRemoveSuppressionOptions::new().add_emails(vec!["emails"]);
    /// let _tmp = BatchRemoveSuppressionOptions::new().add_ids(vec!["ids"]);
    /// ```
    ///
    /// <https://resend.com/docs/api-reference/suppressions/remove-suppressions>
    #[maybe_async::maybe_async]
    #[allow(clippy::needless_pass_by_value, private_bounds)]
    pub async fn batch_remove<M: SpecifiedMarker>(
        &self,
        ids_or_emails: BatchRemoveSuppressionOptions<M>,
    ) -> Result<BatchRemoveSuppressionsResponse> {
        let request = self.0.build(Method::POST, "/suppressions/batch/remove");
        let response = self.0.send(request.json(&ids_or_emails)).await?;
        let content = response.json::<BatchRemoveSuppressionsResponse>().await?;

        Ok(content)
    }
}

#[allow(unreachable_pub)]
pub mod types {
    use std::borrow::ToOwned;

    use serde::{Deserialize, Serialize};

    use crate::types::EmailId;

    crate::define_id_type!(SuppressionId);

    #[must_use]
    #[derive(Debug, Default, Clone, Serialize)]
    pub struct AddSuppressionOptions {
        email: String,
    }

    impl AddSuppressionOptions {
        #[inline]
        pub fn new() -> Self {
            Self::default()
        }

        #[inline]
        pub fn with_email(mut self, email: &str) -> Self {
            email.clone_into(&mut self.email);
            self
        }
    }

    #[must_use]
    #[derive(Debug, Clone, Deserialize)]
    pub struct AddSuppressionResponse {
        pub id: SuppressionId,
    }

    #[must_use]
    #[derive(Debug, Clone, Deserialize)]
    pub struct Suppression {
        pub id: SuppressionId,
        pub email: String,
        pub created_at: String,
        pub origin: SuppressionOrigin,
        pub source_id: Option<EmailId>,
    }

    #[must_use]
    #[derive(Debug, Clone, Copy, Deserialize)]
    #[serde(rename_all = "snake_case")]
    pub enum SuppressionOrigin {
        Bounce,
        Complaint,
        Manual,
    }

    #[must_use]
    #[derive(Debug, Clone, Deserialize)]
    pub struct RemoveSuppressionResponse {
        pub id: SuppressionId,
        pub deleted: bool,
    }

    #[must_use]
    #[derive(Debug, Default, Clone, Serialize)]
    pub struct BatchAddSuppressionOptions {
        emails: Vec<String>,
    }

    impl BatchAddSuppressionOptions {
        #[inline]
        pub fn new() -> Self {
            Self::default()
        }

        #[inline]
        pub fn add_email(mut self, email: &str) -> Self {
            self.emails.push(email.to_owned());
            self
        }

        #[inline]
        pub fn add_emails(mut self, emails: impl IntoIterator<Item = impl Into<String>>) -> Self {
            self.emails.extend(emails.into_iter().map(Into::into));
            self
        }
    }

    impl From<Vec<String>> for BatchAddSuppressionOptions {
        fn from(value: Vec<String>) -> Self {
            Self::new().add_emails(value)
        }
    }
    impl From<Vec<&str>> for BatchAddSuppressionOptions {
        fn from(value: Vec<&str>) -> Self {
            Self::new().add_emails(value.into_iter().map(ToOwned::to_owned))
        }
    }

    #[must_use]
    #[derive(Debug, Clone, Deserialize)]
    pub struct BatchAddSuppressionResponse {
        pub data: Vec<AddSuppressionResponse>,
    }

    #[allow(clippy::redundant_pub_crate)]
    pub(crate) trait SpecifiedMarker {}
    impl SpecifiedMarker for EmailsSpecified {}
    impl SpecifiedMarker for IdsSpecified {}

    #[derive(Debug, Clone, Copy, Default)]
    pub struct NotSpecified {}

    #[derive(Debug, Clone, Copy)]
    pub struct EmailsSpecified {}

    #[derive(Debug, Clone, Copy)]
    pub struct IdsSpecified {}

    #[must_use]
    #[derive(Debug, Default, Clone, Serialize)]
    pub struct BatchRemoveSuppressionOptions<Marker = NotSpecified> {
        #[serde(skip)]
        marker: std::marker::PhantomData<Marker>,

        #[serde(skip_serializing_if = "Vec::is_empty")]
        emails: Vec<String>,
        #[serde(skip_serializing_if = "Vec::is_empty")]
        ids: Vec<String>,
    }

    impl BatchRemoveSuppressionOptions {
        #[inline]
        pub fn new() -> Self {
            Self {
                marker: std::marker::PhantomData::<NotSpecified>,
                emails: vec![],
                ids: vec![],
            }
        }
    }

    impl BatchRemoveSuppressionOptions<NotSpecified> {
        #[inline]
        pub fn add_emails(
            self,
            emails: impl IntoIterator<Item = impl Into<String>>,
        ) -> BatchRemoveSuppressionOptions<EmailsSpecified> {
            BatchRemoveSuppressionOptions::<EmailsSpecified> {
                marker: std::marker::PhantomData,
                emails: emails.into_iter().map(Into::into).collect(),
                ids: self.ids,
            }
        }

        #[inline]
        pub fn add_ids(
            self,
            ids: impl IntoIterator<Item = impl Into<String>>,
        ) -> BatchRemoveSuppressionOptions<IdsSpecified> {
            BatchRemoveSuppressionOptions::<IdsSpecified> {
                marker: std::marker::PhantomData,
                emails: self.emails,
                ids: ids.into_iter().map(Into::into).collect(),
            }
        }
    }

    #[must_use]
    #[derive(Debug, Clone, Deserialize)]
    pub struct BatchRemoveSuppressionsResponse {
        pub data: Vec<RemoveSuppressionResponse>,
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {
    use crate::{
        list_opts::ListResponse,
        test::{CLIENT, DebugResult},
        types::{
            AddSuppressionResponse, BatchAddSuppressionResponse, BatchRemoveSuppressionsResponse,
            RemoveSuppressionResponse, Suppression,
        },
    };

    #[test]
    fn deserialize() {
        let add_suppression = r#"{
        "object": "suppression",
        "id": "e169aa45-1ecf-4183-9955-b1499d5701d3"
      }"#;
        let get_suppression = r#"{
        "object": "suppression",
        "id": "e169aa45-1ecf-4183-9955-b1499d5701d3",
        "email": "steve.wozniak@example.com",
        "origin": "bounce",
        "source_id": "4ef9a417-02e9-4d39-ad75-9611e0fcc33c",
        "created_at": "2026-10-06T23:47:56.678Z"
      }"#;
        let list_suppressions = r#"{
        "object": "list",
        "has_more": false,
        "data": [
          {
            "object": "suppression",
            "id": "e169aa45-1ecf-4183-9955-b1499d5701d3",
            "email": "steve.wozniak@example.com",
            "origin": "manual",
            "source_id": null,
            "created_at": "2026-10-06T23:47:56.678Z"
          },
          {
            "object": "suppression",
            "id": "520784e2-887d-4c25-b53c-4ad46ad38100",
            "email": "susan.kare@example.com",
            "origin": "bounce",
            "source_id": "4ef9a417-02e9-4d39-ad75-9611e0fcc33c",
            "created_at": "2026-10-07T08:12:03.412Z"
          }
        ]
      }"#;
        let remove_suppression = r#"{
        "object": "suppression",
        "id": "e169aa45-1ecf-4183-9955-b1499d5701d3",
        "deleted": true
      }"#;
        let add_suppressions = r#"{
        "data": [
          {
            "object": "suppression",
            "id": "e169aa45-1ecf-4183-9955-b1499d5701d3"
          },
          {
            "object": "suppression",
            "id": "520784e2-887d-4c25-b53c-4ad46ad38100"
          }
        ]
      }"#;
        let remove_suppressions = r#"{
        "data": [
          {
            "object": "suppression",
            "id": "e169aa45-1ecf-4183-9955-b1499d5701d3",
            "deleted": true
          }
        ]
      }"#;

        let res = serde_json::from_str::<AddSuppressionResponse>(add_suppression);
        assert!(res.is_ok());
        let res = serde_json::from_str::<Suppression>(get_suppression);
        assert!(res.is_ok());
        let res = serde_json::from_str::<ListResponse<Suppression>>(list_suppressions);
        assert!(res.is_ok());
        let res = serde_json::from_str::<RemoveSuppressionResponse>(remove_suppression);
        assert!(res.is_ok());
        let res = serde_json::from_str::<BatchAddSuppressionResponse>(add_suppressions);
        assert!(res.is_ok());
        let res = serde_json::from_str::<BatchRemoveSuppressionsResponse>(remove_suppressions);
        assert!(res.is_ok());
    }

    #[tokio_shared_rt::test(shared = true)]
    #[cfg(not(feature = "blocking"))]
    async fn all() -> DebugResult<()> {
        use crate::{
            list_opts::ListOptions,
            types::{
                AddSuppressionOptions, BatchAddSuppressionOptions, BatchRemoveSuppressionOptions,
            },
        };

        let resend = &*CLIENT;
        std::thread::sleep(std::time::Duration::from_secs(1));

        // Add
        let opts = AddSuppressionOptions::new().with_email("steve.wozniak@example.com");
        let suppression = resend.suppressions.add(opts).await?;
        std::thread::sleep(std::time::Duration::from_secs(2));

        // Get
        let suppression = resend.suppressions.get(&suppression.id).await?;

        // List
        let suppressions = resend.suppressions.list(ListOptions::default()).await?;
        assert_eq!(suppressions.len(), 1);

        // Remove
        let removed = resend.suppressions.remove(&suppression.id).await?;
        assert!(removed.deleted);

        std::thread::sleep(std::time::Duration::from_secs(2));

        let suppressions = resend.suppressions.list(ListOptions::default()).await?;
        assert_eq!(suppressions.len(), 0);

        // Batch add
        let opts = vec!["steve.wozniak@example.com", "susan.kare@example.com"];
        let batch_add = resend
            .suppressions
            .batch_add(BatchAddSuppressionOptions::from(opts))
            .await?;
        assert_eq!(batch_add.data.len(), 2);

        std::thread::sleep(std::time::Duration::from_secs(2));

        let suppressions = resend.suppressions.list(ListOptions::default()).await?;
        assert_eq!(suppressions.len(), 2);

        // Batch remove
        let opts = batch_add
            .data
            .into_iter()
            .map(|el| el.id.to_string())
            .collect::<Vec<_>>();
        let batch_remove = resend
            .suppressions
            .batch_remove(BatchRemoveSuppressionOptions::new().add_ids(opts))
            .await?;
        assert_eq!(batch_remove.data.len(), 2);

        std::thread::sleep(std::time::Duration::from_secs(2));

        let suppressions = resend.suppressions.list(ListOptions::default()).await?;
        assert_eq!(suppressions.len(), 0);

        Ok(())
    }
}