Back to Novu

NovuProvider

docs/platform/sdks/react/hooks/novu-provider.mdx

3.18.02.8 KB
Original Source

The NovuProvider is the top-level component that provides the Novu instance to the rest of the hooks through the context. Usually, it's placed somewhere in the root of your application, which makes the hooks accessible throughout the application.

Props

PropertyTypeDescription
subscriberIdstringThe unique identifier of the subscriber
applicationIdentifierstringYour application identifier from Novu
subscriberHashstringHMAC encryption hash for the subscriber
contextobjectDefines contextual attributes (for example, tenant, app, team) for filtering and personalizing notifications.
contextHashstringThe HMAC-SHA256 hash of the context object. Required if context is provided and HMAC is enabled.
apiUrlstringCustom backend URL for self-hosted instances
socketUrlstringCustom socket URL for self-hosted instances
childrenReactNodeThe child components that will have access to the Novu context

Example Usage

<Tabs> <Tab title="US"> ```tsx
function App() {
  return (
    <NovuProvider
      subscriber="SUBSCRIBER_ID"
      applicationIdentifier="APPLICATION_IDENTIFIER"
    >
    </NovuProvider>
  );
}
```
</Tab> <Tab title="EU"> ```tsx
function App() {
  return (
    <NovuProvider
      subscriber="SUBSCRIBER_ID"
      applicationIdentifier="APPLICATION_IDENTIFIER"
      apiUrl="https://eu.api.novu.co"
      socketUrl="wss://eu.socket.novu.co"
    >
    </NovuProvider>
  );
}
```
</Tab> <Tab title="Using Context"> <Note> Learn how to use Context for filtering and personalization in the [Inbox withContext](/platform/inbox/configuration/inbox-with-context) guide. </Note> ```tsx
function App() {
  return (
  <NovuProvider
    applicationIdentifier="APPLICATION_IDENTIFIER"
    subscriber="SUBSCRIBER_ID"
    context={{
      tenant: { id: "org-123" }
    }}
  >
    <App />
  </NovuProvider>
  );
}

// In child components
const MyComponent = () => {
  const novu = useNovu();
  console.log(novu.context);
  // Logs: { tenant: { id: "org-123" } }
};
```
</Tab> <Tab title="HMAC Encryption"> <Note> Read more about [HMAC Encryption](/platform/inbox/prepare-for-production#secure-your-inbox-with-hmac-encryption). </Note>
```tsx

function App() {
  return (
    <NovuProvider
      subscriber="SUBSCRIBER_ID"
      applicationIdentifier="APPLICATION_IDENTIFIER"
      subscriberHash="SUBSCRIBER_HASH_HMAC_ENCRYPTION"
    >
    </NovuProvider>
  );
}
```
</Tab> </Tabs>