> For the complete documentation index, see [llms.txt](https://docs.50spays.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.50spays.com/ko/security/encryption-public-key.md). Agents can also search and read this documentation through its [MCP server](https://docs.50spays.com/~gitbook/mcp).

# Encryption: public key

## Public key

<mark style="color:green;">`GET`</mark> /api/merchant/encryption/public-key

Returns the public key used to encrypt sensitive payment data (e.g., card details) on the client side.

The retrieved public key must be used to encrypt cardholder data before submitting it to the payment API. Encryption must follow the specifications described in the encryption guidelines.

The server will only accept properly encrypted data generated using the provided public key.

**Response**

{% tabs %}
{% tab title="200" %}

```json
{
    "ok": true,
    "encryptionKey": {
        "jwk": {
            "kty": "RSA",
            "n": "z1TINBUZi8.....kx3-O1PM4inohbg9aWQw",
            "e": "AQAB",
            "alg": "RSA-OAEP-256",
            "use": "enc"
        },
        "pem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhk.....+O1PM4inohbg9aW\nQwIDAQAB\n-----END PUBLIC KEY-----\n",
        "alg": "RSA-OAEP-256"
    }
}
```

| Name          | Type   | Description |
| ------------- | ------ | ----------- |
| encryptionKey | object | Public key  |
| {% endtab %}  |        |             |

{% tab title="4xx/5xx" %}

```json
{
    "ok": false,
    "code": 402,
    "message": "human readable error message"
}
```

**All Error Codes**
{% endtab %}
{% endtabs %}

### Encryption Example (Node.js)

This section provides a reference implementation demonstrating how to correctly retrieve the public encryption key and encrypt sensitive card data using RSA-OAEP-256 before submitting it to the API.

The example illustrates:\
• Fetching the public key from the /api/merchant/encryption/public-key endpoint\
• Encrypting cardholder data using RSA-OAEP with SHA-256\
• Encoding the encrypted payload in Base64 for transmission

This sample is provided for integration guidance purposes.

{% code title="Node.js" %}

```js
import crypto from "crypto";

async function importPublicKey(apiEndpoint) {
    const response = await fetch(apiEndpoint);
    const data = await response.json();

    if (!data.ok) {
        throw new Error("Failed to fetch encryption key");
    }

    return data.encryptionKey.pem;
}

function encryptMessage(message, publicKey) {
    const buffer = Buffer.from(message, "utf8");

    const encrypted = crypto.publicEncrypt(
        {
            key: publicKey,
            padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
            oaepHash: "sha256", // потому что RSA-OAEP-256
        },
        buffer
    );

    return encrypted.toString("base64");
}


(async () => {
    const cardCredentials = JSON.stringify({
        pan: "4111111111111111",
        cvv: "444",
        expMonth: 11,
        expYear: 2030,
    });

    const apiEndpoint = `/api/merchant/encryption/public-key`;

    const publicKey = await importPublicKey(apiEndpoint);
    const encryptedBase64 = encryptMessage(cardCredentials, publicKey);

    console.log({ encryptedCardData: encryptedBase64 });
})();

```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.50spays.com/ko/security/encryption-public-key.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
