---
title: "nextmail.provider_imports"
description: "Every method in this namespace: its signature, its parameters, what it returns and an example."
url: "https://nextmail.sh/docs/python/reference/provider-imports"
area: "Python"
category: "Reference"
---

# nextmail.provider_imports

Every method in this namespace: its signature, its parameters, what it returns and an example.

## Methods

Move from Resend, SendGrid, Postmark, Mailgun or Mailchimp: suppressions, audiences, contacts, templates and webhooks come across with one key, with a checklist for domains and API keys.

### `provider_imports.inspect()`

Look inside an account at another provider before importing

```python
def inspect(
    provider_key: str,
    *,
    provider: ProviderImportProvider | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> ProviderInventoryResource
```

Reads the first page of every resource with a key from Resend, SendGrid, Postmark, Mailgun or Mailchimp and returns a count for each, with `more` set where a resource runs past its first page. `resources` lists what this key can bring across: what the provider has no such thing for, or the key may not read, is left out. Nothing is stored, and the key is not kept.

`provider=` picks the provider and defaults to `'resend'`. Resend needs a full-access key, SendGrid a key with read access, Postmark a Server API token, Mailgun a private API key from either region and Mailchimp an API key with its datacenter suffix. A key that can read nothing but sending raises a 400 `restricted_key`: create one with more access for the import and revoke it afterwards.

Scopes: `settings:write`.

**Parameters**

- `provider_key` (`str`, required): The key from the other provider: a full-access Resend key `re_…`, a SendGrid key `SG.…`, a Postmark Server API token, a Mailgun private API key or a Mailchimp key ending in its datacenter, such as `-us6`. The SDK sends it as `apiKey` in the request body.
- `provider` (`ProviderImportProvider`): One of `'resend'`, `'sendgrid'`, `'postmark'`, `'mailgun'` and `'mailchimp'`, the values of `PROVIDER_IMPORT_PROVIDERS`. Left out, the server takes `'resend'`.
- `api_key` (`str`): Overrides the Nextmail API key of the client for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`ProviderInventoryResource`, a dict with `object`, `provider`, `resources` and `counts`, which maps each resource name to a dict with `count` and `more`.

**Example**

```python
from nextmail import nextmail

inventory = nextmail.provider_imports.inspect('SG.123456789', provider='sendgrid')

print(inventory['resources'])
for resource, found in inventory['counts'].items():
    suffix = ' or more' if found['more'] else ''
    print(f'{resource}: {found["count"]}{suffix}')
```

**Notes**

- A key limited to particular addresses or domains is refused with 422 `capability_unsupported`, since audiences, suppressions and templates belong to the whole workspace.
- A key the provider does not accept raises a 400 `invalid_key`, and a provider that cannot be reached a 503 `unreachable`.
- Postmark keeps domains and sender signatures behind an Account token, which the import does not take, so they are not in `resources`. Mailchimp has no webhooks for sending events and does not list API keys.

Also available in: API [`POST /provider-imports/inspect`](https://nextmail.sh/docs/api/reference/imports#post-provider-imports-inspect); TypeScript [`nextmail.providerImports.inspect()`](https://nextmail.sh/docs/sdk/reference/provider-imports#inspect); Ruby [`provider_imports.inspect`](https://nextmail.sh/docs/ruby/reference/provider-imports#inspect); PHP [`providerImports->inspect`](https://nextmail.sh/docs/php/reference/provider-imports#inspect); Go [`ProviderImports.Inspect`](https://nextmail.sh/docs/go/reference/provider-imports#inspect); Java [`providerImports().inspect`](https://nextmail.sh/docs/java/reference/provider-imports#inspect); C# [`ProviderImports.InspectAsync`](https://nextmail.sh/docs/csharp/reference/provider-imports#inspect); CLI [`nextmail provider-imports inspect`](https://nextmail.sh/docs/cli/reference/provider-imports#provider-imports-inspect).

### `provider_imports.create()`

Import from another provider

```python
def create(
    body: ProviderImportCreate,
    *,
    api_key: str | None = None,
    timeout: float | None = None,
) -> ProviderImportResource
```

Queues an import from Resend, SendGrid, Postmark, Mailgun or Mailchimp. Suppressions keep their reason, segments and lists become audiences, subscribed contacts arrive with their names, templates arrive as HTML templates with their variables declared, and webhooks are registered switched off because deliveries here are signed differently. A contact who unsubscribed at the provider is not imported and is recorded as unsubscribed, so `suppressions.list_unsubscribes` lists them. Domains and API keys come back as a checklist in the report, since a signing key or a token cannot leave the provider that made it. Resources the provider has no such thing for, or the key cannot read, are left out, and `resources` on the import says what it brings.

The key is held encrypted only while the import runs and is erased when it ends, fails or is cancelled. Running it twice duplicates nothing.

Scopes: `settings:write`.

**Parameters**

- `body['apiKey']` (`str`, required): The key from the other provider, as `inspect` takes it.
- `body['provider']` (`ProviderImportProvider`): One of `'resend'`, `'sendgrid'`, `'postmark'`, `'mailgun'` and `'mailchimp'`. Left out, the server takes `'resend'`.
- `body['resources']` (`list[ProviderImportResourceKind]`, required): A list of one or more of `'suppressions'`, `'audiences'`, `'contacts'`, `'templates'`, `'webhooks'`, `'domains'` and `'api-keys'`.
- `api_key` (`str`): Overrides the Nextmail API key of the client for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`ProviderImportResource` with `status` set to `'queued'`.

**Example**

```python
from nextmail import nextmail

job = nextmail.provider_imports.create(
    {
        'apiKey': 're_123456789',
        'provider': 'resend',
        'resources': [
            'suppressions',
            'audiences',
            'contacts',
            'templates',
            'webhooks',
            'domains',
            'api-keys',
        ],
    }
)

print(job['id'], job['status'])
```

**Notes**

- One import runs per workspace at a time, and a second raises a 409 `already_running`.
- Contacts who unsubscribed at the provider are left out and counted in `report['contacts']['unsubscribed']`.
- SendGrid hands out contacts through an export it prepares first, so the contacts step waits for it before anything arrives.
- Besides `settings:write`, the key needs the scope behind each resource it brings across: `audiences:write` for audiences, `contacts:write` and `audiences:write` for contacts, `templates:write`, `webhooks:write` and `domains:read`. A missing one is a 403 `insufficient_scope`.
- Not retried by the SDK. Poll `get` with the returned `id` rather than calling it again.

Also available in: API [`POST /provider-imports`](https://nextmail.sh/docs/api/reference/imports#post-provider-imports); TypeScript [`nextmail.providerImports.create()`](https://nextmail.sh/docs/sdk/reference/provider-imports#create); Ruby [`provider_imports.create`](https://nextmail.sh/docs/ruby/reference/provider-imports#create); PHP [`providerImports->create`](https://nextmail.sh/docs/php/reference/provider-imports#create); Go [`ProviderImports.Create`](https://nextmail.sh/docs/go/reference/provider-imports#create); Java [`providerImports().create`](https://nextmail.sh/docs/java/reference/provider-imports#create); C# [`ProviderImports.CreateAsync`](https://nextmail.sh/docs/csharp/reference/provider-imports#create); CLI [`nextmail provider-imports create`](https://nextmail.sh/docs/cli/reference/provider-imports#provider-imports-create).

### `provider_imports.list()`

List one page of imports from sending providers

```python
def list(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Page[ProviderImportResource]
```

One page of the provider imports in the workspace, newest first, each with its report. Nothing is dropped from the history: follow `nextCursor` while `hasMore` is `True` to reach the first one, or let `list_all` and `iterate` do that walk.

Scopes: `settings:read`.

**Parameters**

- `limit` (`int`): Rows per page, a whole number from 1 to 100. The server defaults to 25.
- `cursor` (`str`): The `nextCursor` from the previous page. One that names no provider import in the workspace raises a 400 `invalid_cursor`.
- `api_key` (`str`): Overrides the Nextmail API key of the client for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Page[ProviderImportResource]`, a dict with `items`, `hasMore` and `nextCursor`.

**Example**

```python
from nextmail import nextmail

page = nextmail.provider_imports.list(limit=10)

for job in page['items']:
    print(job['id'], job['status'], job['createdAt'])

print(page['hasMore'], page['nextCursor'])
```

Also available in: API [`GET /provider-imports`](https://nextmail.sh/docs/api/reference/imports#get-provider-imports); TypeScript [`nextmail.providerImports.list()`](https://nextmail.sh/docs/sdk/reference/provider-imports#list); Ruby [`provider_imports.list`](https://nextmail.sh/docs/ruby/reference/provider-imports#list); PHP [`providerImports->list`](https://nextmail.sh/docs/php/reference/provider-imports#list); Go [`ProviderImports.List`](https://nextmail.sh/docs/go/reference/provider-imports#list); Java [`providerImports().list`](https://nextmail.sh/docs/java/reference/provider-imports#list); C# [`ProviderImports.ListAsync`](https://nextmail.sh/docs/csharp/reference/provider-imports#list); CLI [`nextmail provider-imports list`](https://nextmail.sh/docs/cli/reference/provider-imports#provider-imports-list).

### `provider_imports.list_all()`

Collect every provider import into one list

```python
def list_all(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> builtins.list[ProviderImportResource]
```

Follows `nextCursor` from page to page and returns every provider import in the workspace as one list, newest first, each with its report.

Scopes: `settings:read`.

**Parameters**

- `limit` (`int`): Page size per request, from 1 to 100, defaulting to 25 on the server.
- `cursor` (`str`): A provider import id to start after.
- `api_key` (`str`): Overrides the Nextmail API key of the client for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`list[ProviderImportResource]` holding every provider import across all pages.

**Example**

```python
from nextmail import nextmail

jobs = nextmail.provider_imports.list_all()

completed = [job for job in jobs if job['status'] == 'completed']
print(f'{len(completed)} of {len(jobs)} imports completed')
```

**Notes**

- A failure on any page raises out of the whole call, and the imports already fetched are discarded.
- `timeout=` applies to each page request on its own, not to the whole walk.

Also available in: API [`GET /provider-imports`](https://nextmail.sh/docs/api/reference/imports#get-provider-imports); TypeScript [`nextmail.providerImports.listAll()`](https://nextmail.sh/docs/sdk/reference/provider-imports#listAll); Ruby [`provider_imports.list_all`](https://nextmail.sh/docs/ruby/reference/provider-imports#listAll); PHP [`providerImports->listAll`](https://nextmail.sh/docs/php/reference/provider-imports#listAll); Go [`ProviderImports.ListAll`](https://nextmail.sh/docs/go/reference/provider-imports#listAll); Java [`providerImports().listAll`](https://nextmail.sh/docs/java/reference/provider-imports#listAll); C# [`ProviderImports.ListAllAsync`](https://nextmail.sh/docs/csharp/reference/provider-imports#listAll).

### `provider_imports.iterate()`

Stream provider imports one at a time

```python
def iterate(
    *,
    limit: int | None = None,
    cursor: str | None = None,
    api_key: str | None = None,
    timeout: float | None = None,
) -> Iterator[ProviderImportResource]
```

Returns a generator that yields provider imports one at a time, newest first, and requests the next page only once the current one is drained. Breaking out of the loop stops the requests.

Scopes: `settings:read`.

**Parameters**

- `limit` (`int`): Page size per request, from 1 to 100, defaulting to 25 on the server.
- `cursor` (`str`): A provider import id to start after.
- `api_key` (`str`): Overrides the Nextmail API key of the client for every page of this walk.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`Iterator[ProviderImportResource]`, a generator yielding one provider import per step.

**Example**

```python
from nextmail import nextmail

for job in nextmail.provider_imports.iterate():
    if job['status'] == 'running':
        print(job['id'], job['step'])
```

**Notes**

- The generator is lazy, so an abandoned loop costs only the pages you consumed.
- A page that fails raises out of the `for` loop, after the imports of the earlier pages have been yielded.

Also available in: API [`GET /provider-imports`](https://nextmail.sh/docs/api/reference/imports#get-provider-imports); TypeScript [`nextmail.providerImports.iterate()`](https://nextmail.sh/docs/sdk/reference/provider-imports#iterate); Ruby [`provider_imports.iterate`](https://nextmail.sh/docs/ruby/reference/provider-imports#iterate); PHP [`providerImports->iterate`](https://nextmail.sh/docs/php/reference/provider-imports#iterate); Go [`ProviderImports.Iterate`](https://nextmail.sh/docs/go/reference/provider-imports#iterate); Java [`providerImports().iterate`](https://nextmail.sh/docs/java/reference/provider-imports#iterate); C# [`ProviderImports.IterateAsync`](https://nextmail.sh/docs/csharp/reference/provider-imports#iterate).

### `provider_imports.get()`

Read one import from a sending provider

```python
def get(
    id: str,
    *,
    api_key: str | None = None,
    timeout: float | None = None,
) -> ProviderImportResource
```

Status, the step it is on and the report so far. Poll it while `status` is `'queued'` or `'running'`.

Scopes: `settings:read`.

**Parameters**

- `id` (`str`, required): Provider import id, `pimp_` followed by 24 hex characters.
- `api_key` (`str`): Overrides the Nextmail API key of the client for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`ProviderImportResource`, a dict with `id`, `provider`, `status`, `resources`, `step`, `report`, `lastError`, `createdAt`, `startedAt` and `finishedAt`.

**Example**

```python
from nextmail import nextmail

job = nextmail.provider_imports.get('pimp_3f9c2a7b1e4d8f60a5c7b92d')

contacts = job['report']['contacts']
print(job['status'], job['step'], contacts['added'], contacts['unsubscribed'])
```

**Notes**

- An id that names no import in the workspace raises a 404, so `is_not_found` is the check to make.

Also available in: API [`GET /provider-imports/{id}`](https://nextmail.sh/docs/api/reference/imports#get-provider-imports-id); TypeScript [`nextmail.providerImports.get()`](https://nextmail.sh/docs/sdk/reference/provider-imports#get); Ruby [`provider_imports.get`](https://nextmail.sh/docs/ruby/reference/provider-imports#get); PHP [`providerImports->get`](https://nextmail.sh/docs/php/reference/provider-imports#get); Go [`ProviderImports.Get`](https://nextmail.sh/docs/go/reference/provider-imports#get); Java [`providerImports().get`](https://nextmail.sh/docs/java/reference/provider-imports#get); C# [`ProviderImports.GetAsync`](https://nextmail.sh/docs/csharp/reference/provider-imports#get); CLI [`nextmail provider-imports get`](https://nextmail.sh/docs/cli/reference/provider-imports#provider-imports-get).

### `provider_imports.cancel()`

Cancel an import from a sending provider

```python
def cancel(
    id: str,
    *,
    api_key: str | None = None,
    timeout: float | None = None,
) -> ProviderImportResource
```

Stops the import and erases the provider key. What already came across stays.

Scopes: `settings:write`.

**Parameters**

- `id` (`str`, required): Provider import id, `pimp_` followed by 24 hex characters.
- `api_key` (`str`): Overrides the Nextmail API key of the client for this call only.
- `timeout` (`float`): Seconds this call may take, the response included, before it raises `NextmailNetworkError` with `is_timeout`. It overrides the client's `timeout` for this call, and `0` turns the limit off.

**Returns**

`ProviderImportResource` with `status` set to `'cancelled'`.

**Example**

```python
from nextmail import NextmailApiError, nextmail

try:
    job = nextmail.provider_imports.cancel('pimp_3f9c2a7b1e4d8f60a5c7b92d')
    print(job['status'], job['finishedAt'])
except NextmailApiError as error:
    if not error.is_conflict:
        raise
    print('That import has already finished')
```

**Notes**

- An import that has already finished raises a 409 `not_cancellable`.

Also available in: API [`POST /provider-imports/{id}/cancel`](https://nextmail.sh/docs/api/reference/imports#post-provider-imports-id-cancel); TypeScript [`nextmail.providerImports.cancel()`](https://nextmail.sh/docs/sdk/reference/provider-imports#cancel); Ruby [`provider_imports.cancel`](https://nextmail.sh/docs/ruby/reference/provider-imports#cancel); PHP [`providerImports->cancel`](https://nextmail.sh/docs/php/reference/provider-imports#cancel); Go [`ProviderImports.Cancel`](https://nextmail.sh/docs/go/reference/provider-imports#cancel); Java [`providerImports().cancel`](https://nextmail.sh/docs/java/reference/provider-imports#cancel); C# [`ProviderImports.CancelAsync`](https://nextmail.sh/docs/csharp/reference/provider-imports#cancel); CLI [`nextmail provider-imports cancel`](https://nextmail.sh/docs/cli/reference/provider-imports#provider-imports-cancel).
