このクイック スタートでは、Microsoft Learn Platform API を設定して実行する方法について説明します。これには、コース、ラーニング パス、モジュール、認定を取得する例が含まれています。
[前提条件]
開始する前に、次のものがインストールされていることを確認します。
Node.js (バージョン 18.0.0 以降)
- Node.js は、Web ブラウザーの外部で JavaScript/TypeScript コードを実行できる JavaScript ランタイムです。 サンプル スクリプトを実行し、npm (Node Package Manager) を使用してパッケージを管理する必要があります。
- こちらからダウンロードしてください。https://nodejs.org/
- インストールを確認する:
node --version
Azure CLI
- Azure Command-Line インターフェイス (CLI) は、コマンド ラインから Azure リソースを管理するためのツールです。 このガイドでは、ご本人の ID 認証のために使用されます。これにより、示されているサンプルがユーザーに代わって Learn Platform API にアクセスできるようになります。
- 次からダウンロードします。 https://learn.microsoft.com/cli/azure/install-azure-cli>
- インストールを確認する:
az --version
Azure アカウント
- Microsoft Learn Platform API の認証とアクセスには、アクティブなサブスクリプションを持つ Azure アカウントが必要です。 この API は、有効な Azure 資格情報を必要とする認証に Microsoft Entra ID (旧称 Azure Active Directory) を使用します。 Azure サブスクリプションから認証トークンを取得するコストはありません。
- アクティブな Azure サブスクリプションが必要です
- 登録してください: https://azure.microsoft.com/free/
Visual Studio Code (省略可能)
- Visual Studio Code (VS code) は、ターミナル、IntelliSense、TypeScript が統合された無料の軽量コード エディターです。 任意のテキスト エディターとターミナルを使用できますが、Visual Studio Code では、これらの例を実行するための合理化されたエクスペリエンスが提供されます。
- こちらからダウンロードしてください。https://code.visualstudio.com/
- インストールの確認: Visual Studio Code を開き、 Ctrl + ' キー を押して統合ターミナルを開きます
注
以下に記述したすべてのコマンドは、Git Bash ターミナルで実行する必要があります。 VS Code の統合ターミナル ( Ctrl キーを押しながら')、Windows PowerShell、またはコマンド プロンプトを使用できます。
プロジェクトのセットアップ
新しいプロジェクトを作成し、必要な依存関係をインストールするには、次の手順に従います。
プロジェクトの新しいフォルダーを作成し、そこに移動します。
mkdir learn-api-examples cd learn-api-examples既定の設定で新しい Node.js プロジェクトを初期化します。
-yフラグは、すべての既定値を自動的に受け入れます。npm init -y必要な npm パッケージをインストールします (認証用の
@azure/identity、TypeScript を直接実行するためのtsx、コンパイラtypescript、型定義の@types/node)。npm install @azure/identity tsx typescript @types/nodeexamples.tsという名前の新しいファイルを作成し、次のコードを貼り付けます。 これには、Azure の既定の資格情報を利用した認証と、Learn Platform API で使用できるカタログ機能の例がいくつか含まれます。/** * Microsoft Learn Platform API Examples * API Version: 2023-11-01-preview * Base URL: https://learn.microsoft.com/api/v1 * * Authentication: OAuth2 with scope https://learn.microsoft.com/.default */ import { DefaultAzureCredential } from "@azure/identity"; const API_BASE_URL = "https://learn.microsoft.com/api/v1"; const API_VERSION = "2023-11-01-preview"; // ============================================================================ // AUTHENTICATION HELPER // ============================================================================ async function getAccessToken(): Promise<string> { const credential = new DefaultAzureCredential(); const token = await credential.getToken("https://learn.microsoft.com/.default"); return token.token; } /** * Helper to handle API response with error checking */ async function handleResponse(response: Response, label: string): Promise<void> { if (!response.ok) { const text = await response.text(); console.log(`${label} Error: ${response.status} ${response.statusText}`); console.log("Response:", text || "(empty)"); return; } const text = await response.text(); if (!text) { console.log(`${label}: (empty response)`); return; } try { const data = JSON.parse(text); console.log(`${label}:`, JSON.stringify(data, null, 2)); } catch (e) { console.log(`${label} Parse Error: Invalid JSON`); console.log("Raw response:", text.substring(0, 500)); } } // ============================================================================ // TRAINING ENDPOINTS // ============================================================================ /** * List all courses with optional filters * GET /courses */ async function listCourses(options?: { levels?: string[]; roles?: string[]; products?: string[]; locale?: string; maxpagesize?: number; }): Promise<void> { const token = await getAccessToken(); const params = new URLSearchParams({ "api-version": API_VERSION }); if (options?.levels) params.append("levels", options.levels.join(",")); if (options?.roles) params.append("roles", options.roles.join(",")); if (options?.products) params.append("products", options.products.join(",")); if (options?.locale) params.append("locale", options.locale); if (options?.maxpagesize) params.append("maxpagesize", options.maxpagesize.toString()); const response = await fetch(`${API_BASE_URL}/courses?${params}`, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); await handleResponse(response, "Courses"); } /** * Get a specific course by ID * GET /courses/{id} */ async function getCourse(courseId: string, locale?: string): Promise<void> { const token = await getAccessToken(); const params = new URLSearchParams({ "api-version": API_VERSION }); if (locale) params.append("locale", locale); const response = await fetch(`${API_BASE_URL}/courses/${courseId}?${params}`, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); await handleResponse(response, "Course"); } /** * List all learning paths * GET /learning-paths */ async function listLearningPaths(options?: { levels?: string[]; roles?: string[]; products?: string[]; subjects?: string[]; locale?: string; maxpagesize?: number; }): Promise<void> { const token = await getAccessToken(); const params = new URLSearchParams({ "api-version": API_VERSION }); if (options?.levels) params.append("levels", options.levels.join(",")); if (options?.roles) params.append("roles", options.roles.join(",")); if (options?.products) params.append("products", options.products.join(",")); if (options?.subjects) params.append("subjects", options.subjects.join(",")); if (options?.locale) params.append("locale", options.locale); if (options?.maxpagesize) params.append("maxpagesize", options.maxpagesize.toString()); const response = await fetch(`${API_BASE_URL}/learning-paths?${params}`, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); await handleResponse(response, "Learning Paths"); } /** * Get a specific learning path by ID * GET /learning-paths/{id} */ async function getLearningPath(learningPathId: string, locale?: string): Promise<void> { const token = await getAccessToken(); const params = new URLSearchParams({ "api-version": API_VERSION }); if (locale) params.append("locale", locale); const response = await fetch(`${API_BASE_URL}/learning-paths/${learningPathId}?${params}`, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); await handleResponse(response, "Learning Path"); } /** * List all modules * GET /modules */ async function listModules(options?: { levels?: string[]; roles?: string[]; products?: string[]; subjects?: string[]; locale?: string; maxpagesize?: number; }): Promise<void> { const token = await getAccessToken(); const params = new URLSearchParams({ "api-version": API_VERSION }); if (options?.levels) params.append("levels", options.levels.join(",")); if (options?.roles) params.append("roles", options.roles.join(",")); if (options?.products) params.append("products", options.products.join(",")); if (options?.subjects) params.append("subjects", options.subjects.join(",")); if (options?.locale) params.append("locale", options.locale); if (options?.maxpagesize) params.append("maxpagesize", options.maxpagesize.toString()); const response = await fetch(`${API_BASE_URL}/modules?${params}`, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); await handleResponse(response, "Modules"); } /** * Get a specific module by ID * GET /modules/{id} */ async function getModule(moduleId: string, locale?: string): Promise<void> { const token = await getAccessToken(); const params = new URLSearchParams({ "api-version": API_VERSION }); if (locale) params.append("locale", locale); const response = await fetch(`${API_BASE_URL}/modules/${moduleId}?${params}`, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); await handleResponse(response, "Module"); } /** * Get a specific unit by ID * GET /units/{id} */ async function getUnit(unitId: string, locale?: string): Promise<void> { const token = await getAccessToken(); const params = new URLSearchParams({ "api-version": API_VERSION }); if (locale) params.append("locale", locale); const response = await fetch(`${API_BASE_URL}/units/${unitId}?${params}`, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); await handleResponse(response, "Unit"); } // ============================================================================ // CREDENTIALS ENDPOINTS // ============================================================================ /** * List all certifications * GET /certifications */ async function listCertifications(options?: { levels?: string[]; roles?: string[]; products?: string[]; subjects?: string[]; locale?: string; maxpagesize?: number; }): Promise<void> { const token = await getAccessToken(); const params = new URLSearchParams({ "api-version": API_VERSION }); if (options?.levels) params.append("levels", options.levels.join(",")); if (options?.roles) params.append("roles", options.roles.join(",")); if (options?.products) params.append("products", options.products.join(",")); if (options?.subjects) params.append("subjects", options.subjects.join(",")); if (options?.locale) params.append("locale", options.locale); if (options?.maxpagesize) params.append("maxpagesize", options.maxpagesize.toString()); const response = await fetch(`${API_BASE_URL}/certifications?${params}`, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); await handleResponse(response, "Certifications"); } /** * Get a specific certification by ID * GET /certifications/{id} */ async function getCertification(certificationId: string, locale?: string): Promise<void> { const token = await getAccessToken(); const params = new URLSearchParams({ "api-version": API_VERSION }); if (locale) params.append("locale", locale); const response = await fetch(`${API_BASE_URL}/certifications/${certificationId}?${params}`, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); await handleResponse(response, "Certification"); } /** * List all exams * GET /exams */ async function listExams(options?: { levels?: string[]; roles?: string[]; products?: string[]; locale?: string; maxpagesize?: number; }): Promise<void> { const token = await getAccessToken(); const params = new URLSearchParams({ "api-version": API_VERSION }); if (options?.levels) params.append("levels", options.levels.join(",")); if (options?.roles) params.append("roles", options.roles.join(",")); if (options?.products) params.append("products", options.products.join(",")); if (options?.locale) params.append("locale", options.locale); if (options?.maxpagesize) params.append("maxpagesize", options.maxpagesize.toString()); const response = await fetch(`${API_BASE_URL}/exams?${params}`, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); await handleResponse(response, "Exams"); } /** * Get a specific exam by ID * GET /exams/{id} */ async function getExam(examId: string, locale?: string): Promise<void> { const token = await getAccessToken(); const params = new URLSearchParams({ "api-version": API_VERSION }); if (locale) params.append("locale", locale); const response = await fetch(`${API_BASE_URL}/exams/${examId}?${params}`, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); await handleResponse(response, "Exam"); } /** * List all applied skills * GET /applied-skills */ async function listAppliedSkills(options?: { levels?: string[]; roles?: string[]; products?: string[]; subjects?: string[]; locale?: string; maxpagesize?: number; }): Promise<void> { const token = await getAccessToken(); const params = new URLSearchParams({ "api-version": API_VERSION }); if (options?.levels) params.append("levels", options.levels.join(",")); if (options?.roles) params.append("roles", options.roles.join(",")); if (options?.products) params.append("products", options.products.join(",")); if (options?.subjects) params.append("subjects", options.subjects.join(",")); if (options?.locale) params.append("locale", options.locale); if (options?.maxpagesize) params.append("maxpagesize", options.maxpagesize.toString()); const response = await fetch(`${API_BASE_URL}/applied-skills?${params}`, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); await handleResponse(response, "Applied Skills"); } /** * Get a specific applied skill by ID * GET /applied-skills/{id} */ async function getAppliedSkill(appliedSkillId: string, locale?: string): Promise<void> { const token = await getAccessToken(); const params = new URLSearchParams({ "api-version": API_VERSION }); if (locale) params.append("locale", locale); const response = await fetch(`${API_BASE_URL}/applied-skills/${appliedSkillId}?${params}`, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); await handleResponse(response, "Applied Skill"); } // ============================================================================ // PAGINATION HELPER // ============================================================================ /** * Helper to handle paginated responses */ async function fetchAllPages<T>(initialUrl: string, token: string): Promise<T[]> { const allItems: T[] = []; let nextLink: string | undefined = initialUrl; while (nextLink) { const response = await fetch(nextLink, { method: "GET", headers: { "Accept": "application/json", "Authorization": `Bearer ${token}` } }); const data = await response.json(); allItems.push(...data.value); nextLink = data.nextLink; } return allItems; } // ============================================================================ // EXAMPLE USAGE // ============================================================================ async function main() { try { // List beginner-level certifications for Azure console.log("=== Listing Azure certifications (beginner level) ==="); await listCertifications({ levels: ["beginner"], products: ["azure"], maxpagesize: 5 }); // List certifications console.log("\n=== Listing certifications ==="); await listCertifications({ maxpagesize: 5 }); // List all Spanish learning paths console.log("\n=== Listing Spanish learning paths ==="); await listLearningPaths({ locale: "es-es", maxpagesize: 5 }); // Get a specific learning path by ID console.log("\n=== Getting learning path: learn.introduction-ai-azure ==="); await getLearningPath("learn.introduction-ai-azure"); // Get a specific module by ID console.log("\n=== Getting module: learn.wwl.fundamentals-generative-ai ==="); await getModule("learn.wwl.fundamentals-generative-ai"); // Get a specific unit by ID console.log("\n=== Getting unit: learn.wwl.fundamentals-generative-ai.agents ==="); await getUnit("learn.wwl.fundamentals-generative-ai.agents"); // Get a specific applied skill by ID console.log("\n=== Getting applied skill: applied-skill.deploy-and-configure-azure-monitor ==="); await getAppliedSkill("applied-skill.deploy-and-configure-azure-monitor"); // Get a specific certification by ID console.log("\n=== Getting certification: certification.d365-functional-consultant-customer-service ==="); await getCertification("certification.d365-functional-consultant-customer-service"); // Get a specific exam by ID console.log("\n=== Getting exam: exam.77-881 ==="); await getExam("exam.77-881"); // Get a specific instructor-led course by ID console.log("\n=== Getting instructor-led course: course.ai-900t00 ==="); await getCourse("course.ai-900t00"); } catch (error) { console.error("Error:", error); } } main();
例を認証して実行する
Azure で認証し、コード例を実行するには、次の手順に従います。
Azure CLI にサインインします。 この手順では、Azure 資格情報を入力するブラウザー ウィンドウが開きます。
az login複数のサブスクリプションがある場合は、アクティブなサブスクリプションを設定します。
<your-subscription-name>をサブスクリプション名または ID に置き換えます。az account set --subscription "<your-subscription-name>"あなたの資格情報が Learn Platform API にアクセスできることを確認します。
az account get-access-token --resource https://learn.microsoft.com成功した場合は、
accessTokenフィールドを含む JSON 応答が表示されます。npxを使用してサンプル ファイルを実行し、tsxパッケージを実行します。npx tsx examples.ts(省略可能)出力リダイレクトを使用して、出力をファイルに保存します。
npx tsx examples.ts > output.txt 2>&1
リソースをクリーンアップする
このクイック スタートで作成したリソースが不要になった場合は、それらを削除できます。
プロジェクト フォルダーとそのすべての内容を削除します。
cd .. rm -rf learn-api-examplesWindows PowerShell では、次のコマンドを使用します。
cd .. Remove-Item -Recurse -Force learn-api-examples(省略可能)Azure CLI からサインアウトして、キャッシュされた資格情報を削除します。
az logout
注
このクイック スタートでは、コストが発生する Azure リソースは作成されません。 API 呼び出しでは、認証にのみ既存の Azure ID が使用されます。