# Global Shared Example

> See how to use g.shared to share data across APIs, pre/post-hooks, and custom code in API Maker—enable seamless data flow.

Source: https://docs.apimaker.dev/v4/examples/shared/globalShared.html

> **The code in every language**
>
> Each example is the inside of `main`, written in every [language](https://docs.apimaker.dev/v4/docs/languages/index.html) API Maker runs : pick yours in the tabs, and every page shows it from then on. The types a call takes (`GetAll`, `DeepItem`…) are those of `am` : `from am import GetAll` in Python, `use am::GetAll;` in Rust, `import am.GetAll;` in Java and Kotlin ; PHP and C# have them without an import, Go names them `am.GetAll`.

### Shared data globally

`g.shared` is used to share data between post-hooks, pre-hooks, code, and APIs.

- If the user set a shared variable from the one API response.

**TypeScript**

```typescript
g.shared.lastOutput = g.res.output;
```

**Python**

```python
g.shared['lastOutput'] = g.res.output
```

**PHP**

```php
$g->shared->lastOutput = $g->res->output;
```

**C#**

```csharp
g.Shared["lastOutput"] = g.Res.Output?.DeepClone();
```

**Go**

```go
g.Shared.Set("lastOutput", g.Res.Output())
```

**Rust**

```rust
g.shared.set("lastOutput", g.res.output().clone());
```

**Java**

```java
g.shared.put("lastOutput", g.res.getOutput());
```

**Kotlin**

```kotlin
g.shared["lastOutput"] = g.res.output
```

- User can get that shared variables value in another API.

**TypeScript**

```typescript
const lastOutput = g.shared.lastOutput;
```

**Python**

```python
last_output = g.shared.get('lastOutput')
```

**PHP**

```php
$lastOutput = $g->shared->lastOutput;
```

**C#**

```csharp
var lastOutput = g.Shared.GetValueOrDefault("lastOutput");
```

**Go**

```go
lastOutput := g.Shared.Get("lastOutput")
```

**Rust**

```rust
let last_output = g.shared.get("lastOutput");
```

**Java**

```java
Object lastOutput = g.shared.get("lastOutput");
```

**Kotlin**

```kotlin
val lastOutput = g.shared["lastOutput"]
```

## Related

- [The global object g](https://docs.apimaker.dev/v4/docs/pre-defined-terms/global-object-g.html) · [Custom APIs](https://docs.apimaker.dev/v4/docs/apis-all/custom-apis/user-created-custom-api.html)
