LearnCard Developer Docs
  • ๐Ÿš€Get Started
    • ๐Ÿ‘‹Welcome
    • โญWho are you?
      • Learners & Employees
      • Traditional Educator
      • Non-Traditional Educator
      • Assessment Provider
      • Employer
      • App Developer & EdTech
      • DAO & Communities
      • Content Creators
      • Research Institutions
      • NGOs & Governments
      • Plugfest Partner
        • Guide for Interop Issuers
          • ๐ŸคฝCreating an Interop Issuer
        • Guide for Interop Wallets
    • Protocol Overview
      • The Internet of Education
      • The Learning Economy
      • Learner & Employee Privacy
      • 22nd Century Education
      • The Open Credential Network
      • PVCs
  • ๐Ÿ”ฐLearnCard SDK
    • What is LearnCard?
      • Why a Universal Wallet?
      • Architectural Patterns
      • Production Deployment Guide
      • Troubleshooting Guide
    • LearnCard Core
      • Quick Start
        • Create New Credentials
          • Creating Verifiable Credentials for LearnCard
          • Achievement Types and Categories
          • Custom Types
          • Understanding Boosts
          • Creating Boost Credentials
        • Sign & Send Credentials
        • Accept & Verify Credentials
        • Share & Present Credentials
      • Construction
        • Managing Seed Phrases
        • initLearnCard
        • DIDKit
        • learnCardFromSeed
        • emptyLearnCard
        • IDX Config
      • Control Planes
        • ID
        • Read
        • Store
        • Index
        • Cache
        • Context
      • Plugins
        • Adding Plugins
        • Official Plugins
          • Dynamic Loader
          • Crypto
          • DIDKit
          • DID Key
          • VC
            • Expiration Sub-Plugin
          • VC Resolution
          • VC-Templates
          • VC-API
          • Ceramic
          • IDX
          • VPQR
          • Ethereum
          • CHAPI
          • LearnCard Network
          • LearnCloud
          • LearnCard
          • Claimable Boosts
        • Writing Plugins
          • The Simplest Plugin
          • The Plugin Type
          • The LearnCard Type
          • Implementing Control Planes
          • Implementing Methods
          • The Implicit LearnCard
          • Depending on Plugins
          • Private Fields
          • Publishing a Plugin to NPM
      • URIs
      • CHAPI
        • โญCHAPI Wallet Setup Guide
        • โ†”๏ธTranslating to CHAPI documentation
        • ๐Ÿ–ฅ๏ธDemo Application
        • ๐Ÿ”ฐUsing LearnCard to Interact with a CHAPI Wallet
        • ๐Ÿ“Cheat Sheets
          • Issuers
          • Wallets
      • LearnCard UX
        • Quick Start
        • Components
          • Verifiable Credentials
            • VC Thumbnail
            • VC Thumbnail, Mini
          • LearnCards
            • LearnCard Front
            • LearnCard Back
        • API
      • LearnCard Bridge
      • API
      • Migration Guide
    • LearnCard Network
      • LearnCard Network API
        • Authentication
        • Auth Grants and API Tokens
        • Profile
        • Credentials
        • Boosts
        • Presentations
        • Storage
        • Signing Authorities
        • Notifications
        • API Docs
        • Launch Your Own Network
      • ๐Ÿ”ŒConnect Your Application
    • ConsentFlow
      • Setting Up ConsentFlow with an Independent Network
    • GameFlow
      • Sending xAPI Statements
        • xAPI URIs
      • Reading xAPI Statements
        • Advanced xAPI Statement Queries
      • Consentful "Claim Later" Flow
  • ๐Ÿš€Applications
    • LearnCard
    • SuperSkills!
      • SuperSkills! SDK
        • Digital Wallets
        • Issuing into SuperSkills!
        • ๐ŸฆธCreating a SuperSkills! Issuer
    • Metaversity
    • Admin Dashboard
  • ๐Ÿ”—Resources
    • Github
    • Community
    • ๐Ÿ’…Custom Development
    • Contact Our Team
    • Learning Economy
  • ๐Ÿค–LearnCard Services
    • LearnCard CLI
    • Discord Bot
    • Metamask Snap
  • ๐Ÿ’ธLearnBank SDK
    • Why LearnBank?
  • ๐Ÿ“ŠLearnGraph SDK
    • Why LearnGraph?
Powered by GitBook
On this page

Was this helpful?

  1. LearnCard SDK
  2. LearnCard Network
  3. LearnCard Network API

Authentication

PreviousLearnCard Network APINextAuth Grants and API Tokens

Last updated 28 days ago

Was this helpful?

Authenticating with the LearnCard Network API

To interact with the LearnCard Network API, you can choose one of two ways to authenticate:

  1. Using the LearnCard Network Plugin (@learncard/network-plugin) which handles authentication for you. (Preferred option)

  2. for authentication with API endpoints. (recommended for implementations using REST endpoints).

  3. Directly through the API endpoints using challenge-based DID Authentication. (most complex)

1. Using LearnCard Network Plugin

To authenticate using the LearnCard Network Plugin (@learncard/network-plugin), first install the package:

pnpm install @learncard/network-plugin

Then, either instantiate a LearnCard Network enabled LearnCard, or add the Network Plugin to an existing LearnCard:

import { initLearnCard } from '@learncard/init';
import didkit from '@learncard/didkit-plugin/dist/didkit/didkit_wasm_bg.wasm?url';

const networkLearnCard = await initLearnCard({
    seed,
    network: true,
    didkit,
});
import { initLearnCard } from '@learncard/core'
import { getLearnCardNetworkPlugin } from '@learncard/network-plugin';
import didkit from '@learncard/core/dist/didkit/didkit_wasm_bg.wasm?url';

const lcnAPI = 'https://network.learncard.app/trpc';

const learnCard = await initLearnCard({
    seed,
    didkit,
});

const networkLearnCard learnCard.addPlugin(
    await getLearnCardNetworkPlugin(learnCard, lcnAPI)
);

When using the LearnCard Network Plugin, challenge-based DID Authentication is handled for you, so no further steps are necessary.

2. Using Challenge-based DID Authentication

If you choose to use the API endpoints directly, you'll need to manage challenge-based DID Authentication for each request. Here's a simplified TypeScript example to help you implement this authentication method:

 
async function getClient(
  url = 'https://network.learncard.com/api': string,
  didAuthFunction: (challenge?: string) => Promise<string>
) {
  let challenges: string[] = [];

  const getChallenges = async (amount = 95 + Math.round((Math.random() - 0.5) * 5)): Promise<string[]> => {
    // Call the API to get a list of challenges
    // Replace this line with your preferred way of making API calls
    const response = await fetch(url + "/challenges?amount=" + amount);
    return await response.json();
  };

  challenges = await getChallenges();

  async function getAuthHeaders() {
    if (challenges.length === 0) challenges.push(...(await getChallenges()));
    return { Authorization: `Bearer ${await didAuthFunction(challenges.pop())}` };
  }

  // Use getAuthHeaders in your API calls to set the Authorization header
}

export default getClient;

In this example, we first define a getClient function that takes a url and a didAuthFunction. The didAuthFunction should be an asynchronous function that returns a signed challenge as a string.

The getChallenges function fetches a list of challenges from the API. The getAuthHeaders function generates an Authorization header using the didAuthFunction and a challenge. This header can then be used in your API calls.

API Quick Reference

๐Ÿ”ฐ
Creating a scoped Auth Grant and generating an API Token
  • Authenticating with the LearnCard Network API
  • API Quick Reference
  • GETRequest a list of valid challenges
  • GETGet LCN Did

Request a list of valid challenges

get

Generates an arbitrary number of valid challenges for a did, then returns them

Authorizations
Query parameters
amountinteger ยท max: 100OptionalDefault: 100
Responses
200
Successful response
application/json
Responsestring[]
default
Error response
application/json
get

Get LCN Did

get

Gets the did:web for the LCN itself

Authorizations
Responses
200
Successful response
application/json
Responsestring
default
Error response
application/json
get
GET /api/challenges HTTP/1.1
Host: ..
Authorization: Bearer YOUR_SECRET_TOKEN
Accept: */*
[
  "text"
]
GET /api/did HTTP/1.1
Host: ..
Authorization: Bearer YOUR_SECRET_TOKEN
Accept: */*
text