songlink-api
Version:
NodeJS client to query song.link's API
62 lines (42 loc) • 2.07 kB
Markdown
[song.link](https://song.link) API
===
Simple client to query [song.link](https://song.link)'s API.
## Usage
This library exports the following two functions:
### `getLinks(params: SonglinkRequestParams, apiParams?: SonglinkAPIParams) => Promise<SonglinkResponse>`
`SongLinkParams` is described on song.link's docs for their API. You can check it [here](https://github.com/songlink/docs/blob/master/api-v1-alpha.1.md#response-structuretypes)
`SonglinkAPIParams` is an object containing two optional keys:
- `url`: URL to song.link's API
- `apiKey`: API Key as described [here](https://github.com/songlink/docs/blob/master/api-v1-alpha.1.md#auth)
Example:
```typescript
import songlink from 'songlink-api'
songlink.getLinks({ url: 'https://open.spotify.com/track/5p3LIyy38s0QQNoSTwbZXX' })
.then(response => {
Object.entries(response.linksByPlatform)
.map(([platform, { url }]) => {
console.log(`Link for ${platform} is ${url}`)
})
console.log(`song.link URL is ${response.pageUrl}`)
})
```
### getClient(apiParams: SonglinkAPIParams): (params: SonglinkRequestParams) => Promise<SonglinkResponse>
The types remain the same as in [getLinks](#getlinksparams-songlinkrequestparams-apiparams-songlinkapiparams--promisesonglinkresponse) but this will return a new getLinks function, which does not require you to pass the API parameters every time.
This function is usefull in case you have an API key and intend to call `getLinks` in various places of your code.
Example:
```typescript
import songlink from 'songlink-api'
const myApiKey = process.env.SONGLINK_API_KEY
const getLinks = songlink.getClient({ apiKey: myApiKey })
getLinks({ url: 'https://open.spotify.com/track/5p3LIyy38s0QQNoSTwbZXX' })
.then(response => {
Object.entries(response.linksByPlatform)
.map(([platform, { url }]) => {
console.log(`Link for ${platform} is ${url}`)
})
console.log(`song.link URL is ${response.pageUrl}`)
})
```
## TODOs
- [ ] Allow custom HTTP clients
- [ ] Allow options to be passed directly to axios