Skip to content

Latest commit

 

History

History
104 lines (67 loc) · 3.47 KB

File metadata and controls

104 lines (67 loc) · 3.47 KB
title Quickstart: Connect to Azure Managed Redis with TypeScript in a Node.js app
description In this quickstart, you learn how to create a Node.js app that uses Azure Managed Redis.
ms.date 09/02/2025
ms.topic quickstart
ms.custom
mode-api
devx-track-ts
appliesto
✅ Azure Managed Redis
ms.devlang typescript
ai-usage ai-assisted

Quickstart: Connect to Azure Managed Redis in a Node.js app

In this quickstart, you learn how to use an Azure Managed Redis cache from a Node.js application written in the TypeScript language and authenticate the Redis connection by using Microsoft Entra ID.

Prerequisites

  • Azure subscription - create one for free

  • Install Node.js LTS

  • Install TypeScript

  • Add the packages used in this quickstart to your project:

    npm install redis @redis/entraid @redis/client
  • Authenticate to Azure for your development environment with Azure CLI:

    az login

The Quickstart sample code in this article is available on GitHub.

Create an Azure Managed Redis instance

First, create an Azure Managed Redis cache in the Azure portal.

When you create the cache, Microsoft Entra ID authentication is enabled by default, which makes it secure from the start. For this quickstart, the cache uses a public endpoint. In production, consider using private endpoints and other network controls.

  1. To create a cache with the portal, follow one of these procedures:
  • Azure Managed Redis

    Optionally, you can create a cache by using Azure CLI, PowerShell, or whichever tool you prefer.

  1. Grant yourself data access permissions in the Redis resource.

Code to connect to a Redis cache

In the first part of the TypeScript code sample file, index.ts, configure your connection to the cache:

:::code language="typescript" source="~/azure-cache-redis-samples/quickstart/nodejs/src/index.ts" range="1-66":::

Use the createRedisClient() function to create a node-redis client connection to the Redis cache.

client = createRedisClient();
await client.connect();

Code to test a connection

In the next section, test the connection by using the Redis PING command. The Redis server returns PONG.

:::code language="typescript" source="~/azure-cache-redis-samples/quickstart/nodejs/src/index.ts" range="74-75":::

Code set a key, get a key

In this section, use SET and GET commands to start writing and reading data in the Redis cache in the simplest way.

:::code language="typescript" source="~/azure-cache-redis-samples/quickstart/nodejs/src/index.ts" range="77-81":::

Run the code

Build and run the Node.js application.

tsc
node index.js

The result looks like this:

Ping result: PONG
Set result: OK
Get result: Hello! The cache is working from Node.js!

Here, you can see this code sample in its entirety.

:::code language="typescript" source="~/azure-cache-redis-samples/quickstart/nodejs/src/index.ts":::

[!INCLUDE cache-delete-resource-group]

Related content