# Decrypt Data API

> Decrypt a string made by the encrypt data API, or by a client with the transfer key of your secret, with the decrypt data system API of API Maker.

Source: https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-decrypt-data-api.html

Decrypts what [encrypt data](https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-encrypt-data-api.html) produced, or what a client encrypted with `encryptionAlgorithmFETransfer` and `secretFETransfer` of the secret, and gives the original value back.

| | |
|---|---|
| Method | POST |
| URL | `/api/system-api/admin/decrypt-data` : `admin` is the user path of your account |
| Body | `{ data, algorithm?, pass? }` |
| Answer | `data` : the decrypted value, parsed as JSON when it is JSON |
| From code | [`g.sys.system.decrypt`](https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-decrypt-data-api.html#from-code) |

## The call

**Body**

```json
{ "data": "U2FsdGVkX19iIBA3FQy3OgFXeE4B2cC8lAfaJjCbMmUllceqb58YwvQqU33PkQEQ" }
```

**Answer**

```json
{ "success": true, "statusCode": 200, "data": { "name": "Joseph", "card": "4111 1111 1111 1111" } }
```

- `algorithm` and `pass` work as on encrypt data : give the same ones the text was encrypted with.
- A text which does not decrypt with the key answers `data: null`.

## From code

**TypeScript**

```typescript
const encryptedText = g.req.body.encrypted;
const value = await g.sys.system.decrypt(encryptedText);                                       // with the transfer key of the secret
const mine = await g.sys.system.decrypt(encryptedText, T.EEncryptionAlgorithm.AES, 'my-own-key');  // with the algorithm and the key given
```

**Python**

```python
encrypted_text = g.req.body.encrypted
value = await g.sys.system.decrypt(encrypted_text)                           # with the transfer key of the secret
mine = await g.sys.system.decrypt_with(encrypted_text, 'AES', 'my-own-key')  # with the algorithm and the key given
```

**PHP**

```php
$encryptedText = $g->req->body->encrypted;
$value = $g->sys->system->decrypt($encryptedText);                           // with the transfer key of the secret
$mine = $g->sys->system->decryptWith($encryptedText, 'AES', 'my-own-key');  // with the algorithm and the key given
```

**C#**

```csharp
var encryptedText = g.Req.Body.GetString("encrypted");
var value = await g.Sys.System.Decrypt(encryptedText);                           // with the transfer key of the secret
var mine = await g.Sys.System.DecryptWith(encryptedText, "AES", "my-own-key");  // with the algorithm and the key given
```

**Go**

```go
encryptedText := g.Req.Body().String("encrypted")
value, err := g.Sys.System.Decrypt(encryptedText) // with the transfer key of the secret
if err != nil {
	return nil, err
}
mine, err := g.Sys.System.DecryptWith(encryptedText, "AES", "my-own-key") // with the algorithm and the key given
if err != nil {
	return nil, err
}
```

**Rust**

```rust
let encrypted_text = g.req.body()["encrypted"].as_str().unwrap_or_default().to_owned();
let value = g.sys.system.decrypt(&encrypted_text)?;                                // with the transfer key of the secret
let mine = g.sys.system.decrypt_with(&encrypted_text, "AES", Some("my-own-key"))?;  // with the algorithm and the key given
```

**Java**

```java
String encryptedText = g.req.getBody().getString("encrypted");
Object value = g.sys.system.decrypt(encryptedText);                           // with the transfer key of the secret
Object mine = g.sys.system.decryptWith(encryptedText, "AES", "my-own-key");  // with the algorithm and the key given
```

**Kotlin**

```kotlin
val encryptedText = g.req.body.getString("encrypted")
val value = g.sys.system.decrypt(encryptedText)                           // with the transfer key of the secret
val mine = g.sys.system.decryptWith(encryptedText, "AES", "my-own-key")  // with the algorithm and the key given
```

## Access and settings

- Over HTTP, a system API answers once its [settings](https://docs.apimaker.dev/v4/docs/settings/systemApiSettings.html) give it `apiAccessType: TOKEN_ACCESS` (the token of an API user whose [group](https://docs.apimaker.dev/v4/docs/apis-security/api-group-permission.html) grants this system API, in `x-am-authorization`) or `IS_PUBLIC`. Without settings it is `NO_ACCESS` : your code calls it through `g.sys`, the admin panel tests it, and an HTTP call is refused.
- The settings can also cache the answer or require person tokens (`authProviders`) ; [pre and post hooks](https://docs.apimaker.dev/v4/docs/apis-all/hooks/preHook-api.html) run around it like around any API.
- The [request headers](https://docs.apimaker.dev/v4/docs/apis-all/header/requestHeader.html) apply : `x-am-response-case`, `x-am-content-type-response`, `x-am-internationalization`, `x-am-tenant-username`…

## Errors

| Code | When |
|---|---|
| `400` | The body is wrong : the message names the missing or invalid key. |
| `401` | No valid API user token, or the API is `NO_ACCESS` : `You are not authorized to access this API.` |
| `403` | No group grants this system API. |

## Related

- [All APIs at a glance](https://docs.apimaker.dev/v4/docs/apis-all/overview.html) · [Response format](https://docs.apimaker.dev/v4/docs/apis-all/response-format.html) · [System API settings](https://docs.apimaker.dev/v4/docs/settings/systemApiSettings.html) · [System APIs from code](https://docs.apimaker.dev/v4/examples/sys/system/system.html)
