# Get flag updates

By default, SDKs check for updates to your flag logic in the background. When you save and activate a new commit from the Hypertune UI, SDKs will fetch your new flag logic and use it on the next flag evaluation.

To change the frequency at which the SDK checks for updates, set `initDataRefreshIntervalMs` in your `createSource` options:

{% tabs %}
{% tab title="React" %}
{% code title="src/components/AppHypertuneProvider.tsx" %}

```tsx
import { HypertuneProvider } from '../generated/hypertune.react'

export default function AppHypertuneProvider({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <HypertuneProvider
      createSourceOptions={{
        token: import.meta.env.VITE_HYPERTUNE_TOKEN!,
        initDataRefreshIntervalMs: 5_000,
      }}
      rootArgs={{
        context: {
          environment:
            process.env.NODE_ENV === 'development'
              ? 'development'
              : 'production',
          user: {
            id: 'e23cc9a8-0287-40aa-8500-6802df91e56a',
            name: 'Example User',
            email: 'user@example.com',
          },
        },
      }}
    >
      {children}
    </HypertuneProvider>
  )
}
```

{% endcode %}
{% endtab %}

{% tab title="Node.js" %}
{% code title="src/lib/getHypertune.ts" %}

```typescript
import { createSource } from '../generated/hypertune'

const hypertuneSource = createSource({
  token: process.env.HYPERTUNE_TOKEN!,
  initDataRefreshIntervalMs: 5_000,
})

export default async function getHypertune() {
  // Get flag updates in serverless environments
  // await hypertuneSource.initIfNeeded();

  return hypertuneSource.root({
    args: {
      context: {
        environment:
          process.env.NODE_ENV === 'development'
            ? 'development'
            : 'production',
        user: {
          id: 'e23cc9a8-0287-40aa-8500-6802df91e56a',
          name: 'Example User',
          email: 'user@example.com',
        },
      },
    },
  })
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}

```python
import generated.hypertune as hypertune

root_node = hypertune.create_source(
    init_data_refresh_interval_ms=5_000
).root({
    "context": {
        "environment": "development",
        "user": {
            "id": "test_id",
            "name": "Test",
            "email": "hi@test.com",
        }
    }
})
root_node.wait_for_initialization()
```

{% endtab %}

{% tab title="Rust" %}

```rust
use hypertune::CreateOptions;

mod hypertune;

fn main() {
    let root_node = hypertune::initialize_hypertune(
        hypertune::VariableValues {},
        Some(CreateOptions {
            init_data_refresh_interval_ms: 5_000,
            ..Default::default()
        })
    )
    .unwrap()
    .root(hypertune::RootArgs {
        context: hypertune::Context {
            environment: hypertune::Environment::DEVELOPMENT,
            user: hypertune::User {
                id: "test_id".to_string(),
                name: "Test".to_string(),
                email: "test@test.com".to_string(),
            },
        },
    });
    root_node.wait_for_initialization();
}
```

{% endtab %}

{% tab title="Go" %}

<pre class="language-go"><code class="lang-go"><strong>package main
</strong>
import (
	"fmt"
	"log"
	"os"
	"time"

	sdk "github.com/hypertunehq/hypertune-go"
	// Update to your project path.
	"github.com/myTeam/myProject/pkg/hypertune"
)

func main() {
	var token = os.Getenv("HYPERTUNE_TOKEN")
	source, err := hypertune.CreateSource(
		&#x26;token,
		sdk.WithInitDataRefreshInterval(5*time.Second),
	)
	if err != nil {
		panic(err)
	}
	defer source.Close()
	
	source.WaitForInitialization()
	
	rootNode := source.Root(hypertune.RootArgs{
		Context: hypertune.Context{
			Environment: hypertune.Development,
			User: hypertune.User{
				Id:         "test_id",
				Name:       "Test",
				Email:      "hi@test.com",
			},
		},
	})
}
</code></pre>

{% endtab %}
{% endtabs %}

To disable checking for updates, set `shouldRefreshInitData` in your `createSource` options to `false`:

{% tabs %}
{% tab title="React" %}
{% code title="src/components/AppHypertuneProvider.tsx" %}

```tsx
import { HypertuneProvider } from '../generated/hypertune.react'

export default function AppHypertuneProvider({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <HypertuneProvider
      createSourceOptions={{
        token: import.meta.env.VITE_HYPERTUNE_TOKEN!,
        shouldRefreshInitData: false,
      }}
      rootArgs={{
        context: {
          environment:
            process.env.NODE_ENV === 'development'
              ? 'development'
              : 'production',
          user: {
            id: 'e23cc9a8-0287-40aa-8500-6802df91e56a',
            name: 'Example User',
            email: 'user@example.com',
          },
        },
      }}
    >
      {children}
    </HypertuneProvider>
  )
}
```

{% endcode %}
{% endtab %}

{% tab title="Node.js" %}
{% code title="src/lib/getHypertune.ts" %}

```typescript
import { createSource } from '../generated/hypertune'

const hypertuneSource = createSource({
  token: process.env.HYPERTUNE_TOKEN!,
  shouldRefreshInitData: false,
})

export default async function getHypertune() {
  // Get flag updates in serverless environments
  // await hypertuneSource.initIfNeeded();

  return hypertuneSource.root({
    args: {
      context: {
        environment:
          process.env.NODE_ENV === 'development'
            ? 'development'
            : 'production',
        user: {
          id: 'e23cc9a8-0287-40aa-8500-6802df91e56a',
          name: 'Example User',
          email: 'user@example.com',
        },
      },
    },
  })
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}

```python
import generated.hypertune as hypertune

root_node = hypertune.create_source(
    init_data_refresh_interval_ms=0
).root({
    "context": {
        "environment": "development",
        "user": {
            "id": "test_id",
            "name": "Test",
            "email": "hi@test.com",
        }
    }
})
root_node.wait_for_initialization()
```

{% endtab %}

{% tab title="Rust" %}

```rust
use hypertune::CreateOptions;

mod hypertune;

fn main() {
    let root_node = hypertune::initialize_hypertune(
        hypertune::VariableValues {},
        Some(CreateOptions {
            init_data_refresh_interval_ms: 0,
            ..Default::default()
        })
    )
    .unwrap()
    .root(hypertune::RootArgs {
        context: hypertune::Context {
            environment: hypertune::Environment::DEVELOPMENT,
            user: hypertune::User {
                id: "test_id".to_string(),
                name: "Test".to_string(),
                email: "test@test.com".to_string(),
            },
        },
    });
    root_node.wait_for_initialization();
}
```

{% endtab %}

{% tab title="Go" %}

<pre class="language-go"><code class="lang-go"><strong>package main
</strong>
import (
	"fmt"
	"log"
	"os"

	sdk "github.com/hypertunehq/hypertune-go"
	// Update to your project path.
	"github.com/myTeam/myProject/pkg/hypertune"
)

func main() {
	var token = os.Getenv("HYPERTUNE_TOKEN")
	source, err := hypertune.CreateSource(
		&#x26;token,
		sdk.WithInitDataRefreshInterval(0),
	)
	if err != nil {
		panic(err)
	}
	defer source.Close()
	
	source.WaitForInitialization()
	
	rootNode := source.Root(hypertune.RootArgs{
		Context: hypertune.Context{
			Environment: hypertune.Development,
			User: hypertune.User{
				Id:         "test_id",
				Name:       "Test",
				Email:      "hi@test.com",
			},
		},
	})
}
</code></pre>

{% endtab %}
{% endtabs %}

## Manually check for flag updates

To manually check for flag updates, use the `initIfNeeded` method:

{% code title="src/lib/getHypertune.ts" %}

```typescript
import { createSource } from '../generated/hypertune'

const hypertuneSource = createSource({
  token: process.env.HYPERTUNE_TOKEN!,
  initDataRefreshIntervalMs: 5_000,
})

export default async function getHypertune() {
  await hypertuneSource.initIfNeeded()

  return hypertuneSource.root({
    args: {
      context: {
        environment:
          process.env.NODE_ENV === 'development'
            ? 'development'
            : 'production',
        user: {
          id: 'e23cc9a8-0287-40aa-8500-6802df91e56a',
          name: 'Example User',
          email: 'user@example.com',
        },
      },
    },
  })
}
```

{% endcode %}

When using `initIfNeeded`, the `initDataRefreshIntervalMs` option specifies the minimum time between initialization requests.

For example, if you set this to `5_000`, `initIfNeeded` will only trigger a new initialization request if the last one was over 5 seconds ago.

This means await `initIfNeeded` on every backend request to ensure flag values are fresh while minimizing network latency and bandwidth.

This is particularly useful in [serverless and edge environments](https://docs.hypertune.com/sdk-reference/serverless-environments) like Vercel deployments, Cloudflare Workers, AWS Lambdas, etc, where background SDK tasks like fetching updates aren't guaranteed to execute.

## Subscribe to flag updates

To add and remove listeners to be notified of updates, use the following methods:

```typescript
hypertune.addUpdateListener(listener: (newStateHash: string) => void)
hypertune.removeUpdateListener(listener: (newStateHash: string) => void)
```

To get the current state hash, use the following method:

```typescript
hypertune.getStateHash(): string | null
```
