0

Create Highlights

by
Published Sep 28, 2023
Script readwise
  • Submitted by bryan958 Typescript (fetch-only)
    Created 985 days ago
    1
    type Highlight = {
    2
        text: string,
    3
        title?: string,
    4
        author?: string,
    5
        imagge_url?: string,
    6
        source_url?: string,
    7
        source_type?: string,
    8
        category?: 'books' | 'articles' | 'tweets' | 'podcasts',
    9
        note?: string,
    10
        location?: number,
    11
        location_type?: 'page' | 'order' | 'time_offset',
    12
        highlighted_at?: string,
    13
        highlight_url?: string,
    14
    }
    15
    
    
    16
    export async function main(
    17
      token: string,
    18
      highlights: Highlight[]
    19
    ) {
    20
      const response = await fetch('https://readwise.io/api/v2/highlights/', {
    21
        method: 'POST',
    22
        headers: {
    23
          'Content-Type': 'application/json; charset=utf-8',
    24
          'Authorization': `Token ${token}`,
    25
        },
    26
        body: JSON.stringify({highlights})
    27
      })
    28
      if (response.status !== 200) {
    29
        throw new Error(`failed to insert highlights: invalid response status: code=${response.status}, msg=${await response.text()}`)
    30
      }
    31
    
    
    32
      return await response.json()
    33
    }