---
title: Set Pre-Shared Keys (PSK) for IPsec tunnels
---

[Skip to content](#_top)

[API Reference](https://developers.cloudflare.com/api/python)

[Magic Transit](https://developers.cloudflare.com/api/python/resources/magic_transit)

[IPSEC Tunnels](https://developers.cloudflare.com/api/python/resources/magic_transit/subresources/ipsec_tunnels)

Copy Markdown

Open in **Claude**Open in **ChatGPT**Open in **Cursor**

---

**Copy Markdown****View as Markdown**

# Set Pre-Shared Keys (PSK) for IPsec tunnels

magic\_transit.ipsec\_tunnels.psk\_set(IPSECTunnelPSKSetParams\*\*kwargs) -> [IPSECTunnelPSKSetResponse](<https://developers.cloudflare.com/api/python/resources/magic_transit#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(model)%20ipsec_tunnel_psk_set_response%20%3E%20(schema)>)

POST/accounts/{account\_id}/magic/ipsec\_tunnels/psk

Sets Pre-Shared Keys for multiple IPsec tunnels associated with an account. Use `?validate_only=true` as an optional query parameter to only run validation without persisting changes. After PSKs are applied, they are immediately persisted to Cloudflare’s edge and cannot be retrieved later. Store the PSKs in a safe place.

##### Security

<details>

<summary>API Token</summary>



The preferred authorization scheme for interacting with the Cloudflare API. <a href="https://developers.cloudflare.com/fundamentals/api/get-started/create-token/">Create a token</a>.

**Example:**<code>Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY</code>

</details>

<details>

<summary>API Email + API Key</summary>



The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.

**Example:**<code>X-Auth-Email: user@example.com</code>

The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.

**Example:**<code>X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194</code>

</details>

##### Accepted Permissions (at least one required)

`Magic WAN Write``Magic Transit Write`

##### ParametersExpand Collapse

account\_id: str

Identifier

maxLength32

[Link to this property](<#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(method)%20psk_set%20%3E%20(params)%20default%20%3E%20(param)%20account_id%20%3E%20(schema)>)

<details>

<summary>

psks: Iterable\[PSK]

List of tunnel ID and PSK pairs.

</summary>

id: str

The ID of the IPsec tunnel.

maxLength32

<a href="#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(method)%20psk_set%20%3E%20(params)%20default%20%3E%20(param)%20psks%20%3E%20(schema)%20%3E%20(items)%20%3E%20(property)%20id">Link to this property</a>

psk: str

A randomly generated or provided string for use in the IPsec tunnel.

<a href="#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(method)%20psk_set%20%3E%20(params)%20default%20%3E%20(param)%20psks%20%3E%20(schema)%20%3E%20(items)%20%3E%20(property)%20psk">Link to this property</a>

</details>

[Link to this property](<#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(method)%20psk_set%20%3E%20(params)%20default%20%3E%20(param)%20psks%20%3E%20(schema)>)

validate\_only: Optional\[[bool](<https://developers.cloudflare.com/api/python/resources/magic_transit/subresources/ipsec_tunnels/methods/psk_set#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(method)%20psk_set%20%3E%20(params)%20default%20%3E%20(param)%20validate_only%20%3E%20(schema)>)]

If `true`, only run validation without persisting changes.

[Link to this property](<#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(method)%20psk_set%20%3E%20(params)%20default%20%3E%20(param)%20validate_only%20%3E%20(schema)>)

##### ReturnsExpand Collapse

<details>

<summary>

class IPSECTunnelPSKSetResponse: …

</summary>

<details>

<summary>

successfully\_applied\_psks: Optional\[Dict\[str, SuccessfullyAppliedPSKs]]

Map of tunnel IDs to successfully applied PSK details.

</summary>

ipsec\_id: str

The IKE identifier used for this tunnel on the Cloudflare edge.

<a href="#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(model)%20ipsec_tunnel_psk_set_response%20%3E%20(schema)%20%3E%20(property)%20successfully_applied_psks%20%3E%20(items)%20%3E%20(property)%20ipsec_id">Link to this property</a>

ipsec\_tunnel\_id: str

Identifier

maxLength32

<a href="#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(model)%20ipsec_tunnel_psk_set_response%20%3E%20(schema)%20%3E%20(property)%20successfully_applied_psks%20%3E%20(items)%20%3E%20(property)%20ipsec_tunnel_id">Link to this property</a>

psk: str

A randomly generated or provided string for use in the IPsec tunnel.

<a href="#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(model)%20ipsec_tunnel_psk_set_response%20%3E%20(schema)%20%3E%20(property)%20successfully_applied_psks%20%3E%20(items)%20%3E%20(property)%20psk">Link to this property</a>

<details>

<summary>

psk\_metadata: <a href="https://developers.cloudflare.com/api/python/resources/magic_transit#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(model)%20psk_metadata%20%3E%20(schema)">PSKMetadata</a>

The PSK metadata that includes when the PSK was generated.

</summary>

last\_generated\_on: Optional\[datetime]

The date and time the tunnel was last modified.

formatdate-time

<a href="#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(model)%20ipsec_tunnel_psk_set_response%20%3E%20(schema)%20%3E%20(property)%20successfully_applied_psks%20%3E%20(items)%20%3E%20(property)%20psk_metadata%20%2B%20(resource)%20magic_transit.ipsec_tunnels%20%3E%20(model)%20psk_metadata%20%3E%20(schema)%20%3E%20(property)%20last_generated_on">Link to this property</a>

</details>

<a href="#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(model)%20ipsec_tunnel_psk_set_response%20%3E%20(schema)%20%3E%20(property)%20successfully_applied_psks%20%3E%20(items)%20%3E%20(property)%20psk_metadata">Link to this property</a>

</details>

<a href="#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(model)%20ipsec_tunnel_psk_set_response%20%3E%20(schema)%20%3E%20(property)%20successfully_applied_psks">Link to this property</a>

unapplied\_psks: Optional\[Dict\[str, str]]

Map of tunnel IDs to failure reasons for PSKs that could not be applied.

<a href="#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(model)%20ipsec_tunnel_psk_set_response%20%3E%20(schema)%20%3E%20(property)%20unapplied_psks">Link to this property</a>

</details>

[Link to this property](<#(resource)%20magic_transit.ipsec_tunnels%20%3E%20(model)%20ipsec_tunnel_psk_set_response%20%3E%20(schema)>)

### Set Pre-Shared Keys (PSK) for IPsec tunnels

Python

HTTPTypeScriptPythonGoTerraform

```
import os
from cloudflare import Cloudflare

client = Cloudflare(
    api_token=os.environ.get("CLOUDFLARE_API_TOKEN"),  # This is the default and can be omitted
)
response = client.magic_transit.ipsec_tunnels.psk_set(
    account_id="023e105f4ecef8ad9ca31a8372d0c353",
    psks=[{
        "id": "023e105f4ecef8ad9ca31a8372d0c353",
        "psk": "O3bwKSjnaoCxDoUxjcq4Rk8ZKkezQUiy",
    }],
)
print(response.successfully_applied_psks)
```

200 example

```
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "result": {
    "successfully_applied_psks": {
      "foo": {
        "ipsec_id": "12345_abc123def4567890abcdef1234567890",
        "ipsec_tunnel_id": "023e105f4ecef8ad9ca31a8372d0c353",
        "psk": "O3bwKSjnaoCxDoUxjcq4Rk8ZKkezQUiy",
        "psk_metadata": {
          "last_generated_on": "2017-06-14T05:20:00Z"
        }
      }
    },
    "unapplied_psks": {
      "foo": "string"
    }
  },
  "success": true
}
```

##### Returns Examples

200 example

```
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "result": {
    "successfully_applied_psks": {
      "foo": {
        "ipsec_id": "12345_abc123def4567890abcdef1234567890",
        "ipsec_tunnel_id": "023e105f4ecef8ad9ca31a8372d0c353",
        "psk": "O3bwKSjnaoCxDoUxjcq4Rk8ZKkezQUiy",
        "psk_metadata": {
          "last_generated_on": "2017-06-14T05:20:00Z"
        }
      }
    },
    "unapplied_psks": {
      "foo": "string"
    }
  },
  "success": true
}
```