1+ import HackMDAPI from '@hackmd/api' ;
2+ import dotenv from 'dotenv' ;
3+
4+ // Load environment variables
5+ dotenv . config ( ) ;
6+
7+ // Check for required environment variable
8+ if ( ! process . env . HACKMD_ACCESS_TOKEN ) {
9+ console . error ( 'Error: HACKMD_ACCESS_TOKEN environment variable is not set.' ) ;
10+ console . error ( 'Please set your HackMD access token using one of these methods:' ) ;
11+ console . error ( '1. Create a .env file with HACKMD_ACCESS_TOKEN=your_token_here' ) ;
12+ console . error ( '2. Set the environment variable directly: export HACKMD_ACCESS_TOKEN=your_token_here' ) ;
13+ process . exit ( 1 ) ;
14+ }
15+
16+ // Create API client with retry configuration
17+ const client = new HackMDAPI ( process . env . HACKMD_ACCESS_TOKEN , 'https://api.hackmd.io/v1' , {
18+ retryConfig : {
19+ maxRetries : 3 ,
20+ baseDelay : 100
21+ }
22+ } ) ;
23+
24+ async function main ( ) {
25+ try {
26+ // Example 1: Get user information
27+ console . log ( 'Getting user information...' ) ;
28+ const me = await client . getMe ( ) ;
29+ console . log ( 'User email:' , me . email ) ;
30+ console . log ( 'User name:' , me . name ) ;
31+
32+ // Example 2: Create a new note
33+ console . log ( '\nCreating a new note...' ) ;
34+ const newNote = await client . createNote ( {
35+ title : 'Test Note' ,
36+ content : '# Hello from HackMD API\n\nThis is a test note created using the API client.' ,
37+ readPermission : 'guest' ,
38+ writePermission : 'owner'
39+ } ) ;
40+ console . log ( 'Created note ID:' , newNote . id ) ;
41+ console . log ( 'Note URL:' , newNote . publishLink ) ;
42+
43+ // Example 3: Get note with ETag support
44+ console . log ( '\nGetting note with ETag support...' ) ;
45+ const note = await client . getNote ( newNote . id ) ;
46+ console . log ( 'Note content:' , note . content ) ;
47+
48+ // Second request with ETag
49+ const updatedNote = await client . getNote ( newNote . id , { etag : note . etag } ) ;
50+ console . log ( 'Note status:' , updatedNote . status ) ;
51+
52+ // Example 4: Update note content
53+ console . log ( '\nUpdating note content...' ) ;
54+ const updatedContent = await client . updateNoteContent ( newNote . id , '# Updated Content\n\nThis note has been updated!' ) ;
55+ console . log ( 'Updated note content:' , updatedContent . content ) ;
56+
57+ // Example 5: Get raw response (unwrapData: false)
58+ console . log ( '\nGetting raw response...' ) ;
59+ const rawResponse = await client . getNote ( newNote . id , { unwrapData : false } ) ;
60+ console . log ( 'Response headers:' , rawResponse . headers ) ;
61+ console . log ( 'Response status:' , rawResponse . status ) ;
62+
63+ // Example 6: Delete the test note
64+ console . log ( '\nCleaning up - deleting test note...' ) ;
65+ await client . deleteNote ( newNote . id ) ;
66+ console . log ( 'Note deleted successfully' ) ;
67+
68+ } catch ( error ) {
69+ console . error ( 'Error:' , error . message ) ;
70+ if ( error . response ) {
71+ console . error ( 'Response status:' , error . response . status ) ;
72+ console . error ( 'Response data:' , error . response . data ) ;
73+ }
74+ }
75+ }
76+
77+ main ( ) ;
0 commit comments