# Internal API (trusted Java and hybrid plugins)

The internal API is an explicitly unstable facade over host internals. It exists for
Java and hybrid packages that need behaviour the public v1 SDK cannot express. It is not
part of the stable public contract and host updates may change it without a major SDK
version.

## 1. Who can use it

- Package kind `java` or `hybrid`.
- Declared permission `internal.access` (critical, off by default).
- The kernel switch **Allow internal API access** is on.
- The user granted the permission after a second confirmation.

A `ts`/`script` package that requests `internal.access` is rejected with
`E_PERMISSION`. A Java package without the grant gets `E_PERMISSION` from
`context.require(InternalHost.class)`.

```java
InternalHost host = context.require(InternalHost.class);
host.runOnUiThread(() -> host.navigation().openChat(dialogId));
```

## 2. Surface

`InternalHost` is typed and deliberately small:

| Member | Purpose |
| --- | --- |
| `applicationContext()`, `currentActivity()` | Host context and current activity |
| `runOnUiThread`, `runOnBackground` | Host executors |
| `messages(account)` | Dialog title, last message id, mute state |
| `sendHelper(account)` | Pending-send inspection and cancellation |
| `theme()` | Theme color lookup, dark mode, theme change |
| `chatRights(dialogId)` | Admin/creator/pin/delete/ban/invite/promote/topic checks |
| `navigation()` | Chat, profile, settings, chat edit, members, invite links |
| `viewFactory()` | Framework `TextView`, `ImageView`, `LinearLayout`, spacer |

Static-only upstream namespaces are wrapped, never returned as instances. The facade
does not expose raw `org.telegram.*` types in the public SDK artifact.

## 3. Trust boundary

Installing a Java or hybrid package is equivalent to installing trusted code. A DEX
plugin can resolve host classes because the parent class loader is the app loader; the
host does not pretend to prevent that. `internal.access` gates the supported, typed and
audited route, not raw class resolution. This residual is documented in
[security.md](/security/) and shown on the consent screen.

The public API still filters secret chats and protected surfaces. The internal facade
does not bypass those filters. A malicious Java plugin can reach upstream data directly;
that is the honest trust-tier trade-off, not a sandbox claim.

## 4. Distribution

The artifact is `nextgram-plugin-sdk-internal`. Plugin authors compile against it as
`compileOnly`; the host provides it at runtime. It is version-pinned to the host build
and carries no stability promise. R8 keep rules preserve the facade, its upstream return
types and the methods the host calls.

Use the public API whenever it can express the task. The internal API is the last
resort, not the default.
