Skip to content

Commit 825a39d

Browse files
authored
Support bearer token initialization in SDK (#13741)
1 parent bd7fe99 commit 825a39d

46 files changed

Lines changed: 1360 additions & 851 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/breezy-humans-sip.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@audius/sdk': minor
3+
---
4+
5+
Support bearer token initialization

docs/docs/developers/sdk/overview.mdx

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,26 @@ the SDK to:
2222

2323
...and much more!
2424

25-
## Set Up Your Developer App
25+
## API Plans
2626

27-
1. [Create an Audius account](https://audius.co/signup) if you do not have one already.
27+
Audius offers two API plans:
2828

29-
2. Head to the [Settings page](https://audius.co/settings) and select "Manage Your Apps." Follow the
30-
prompts to create a new developer app and get your Audius API Key and API Secret.
29+
| Plan | Rate Limit | Monthly Requests |
30+
| ------------- | ------------------ | ---------------------- |
31+
| **Free** | 10 requests/second | 500,000 requests/month |
32+
| **Unlimited** | Unlimited | Unlimited |
33+
34+
The Free plan is always free with no restrictions. For higher limits and support, contact [api@audius.co](mailto:api@audius.co) about the Unlimited plan.
35+
36+
## Get Your API Key and Bearer Token
37+
38+
1. Visit the [Audius API Plans page](https://api.audius.co/plans) and click "Create API Key" to generate your credentials.
39+
40+
2. You will receive an **API Key** and a **Bearer Token**. Save both securely — treat them like passwords.
3141

3242
:::tip
3343

34-
Make sure you save your API Secret somewhere safe — treat it like a password.
44+
The bearer token is the recommended way to authenticate with the Audius API.
3545

3646
:::
3747

@@ -45,7 +55,7 @@ Make sure you save your API Secret somewhere safe — treat it like a password.
4555
If your project is in a Node.js environment, run this in your terminal:
4656

4757
```bash
48-
npm install web3 @audius/sdk
58+
npm install @audius/sdk
4959
```
5060

5161
[@audius/sdk on NPM](https://www.npmjs.com/package/@audius/sdk)
@@ -56,16 +66,12 @@ Otherwise, include the SDK script tag in your web page. The Audius SDK will then
5666
`window.audiusSdk`.
5767

5868
```html
59-
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
6069
<script src="https://cdn.jsdelivr.net/npm/@audius/sdk@latest/dist/sdk.min.js"></script>
6170
```
6271

6372
## Initialize the SDK
6473

65-
Initialize the SDK with your API key.
66-
67-
If you plan to write data to Audius (e.g. upload a track, favorite a playlist, etc.), then pass in
68-
your API secret as well.
74+
Initialize the SDK with your API key and bearer token.
6975

7076
### Node.js example
7177

@@ -74,48 +80,42 @@ import { sdk } from '@audius/sdk'
7480

7581
const audiusSdk = sdk({
7682
apiKey: 'Your API Key goes here',
77-
apiSecret: 'Your API Secret goes here',
83+
bearerToken: 'Your Bearer Token goes here',
7884
})
7985
```
8086

8187
### HTML + JS example
8288

8389
```js title="In web page"
84-
const audiusSdk = window.audiusSdk({ apiKey: 'Your API key goes here' })
90+
const audiusSdk = window.audiusSdk({
91+
apiKey: 'Your API Key goes here',
92+
})
8593
```
8694

8795
:::warning
8896

89-
Do NOT include your API secret if you are running the SDK on the frontend, as this will expose your
90-
secret.
97+
DO NOT include the bearer token if you are running the SDK in the browser or anywhere on the client.
98+
The bearer token is what allows your app to write on behalf of the users that have authorized it to
99+
do so. Keep your bearer token secure and never expose it in client-side code that could be inspected.
91100

92101
:::
93102

94103
## Make your first API call using the SDK!
95104

96105
Once you have the initialized SDK instance, it's smooth sailing to making your first API calls.
97106

98-
:::note
99-
100-
If you included your API secret in the previous step, you'll be able do both reads (e.g. fetching a
101-
playlist) and writes (e.g. reposting a playlist) to Audius. Otherwise, you'll be able to read data
102-
only.
103-
104-
:::
105-
106107
```js
107108
// Fetch your first track!
108109
const track = await audiusSdk.tracks.getTrack({ trackId: 'D7KyD' })
109110
console.log(track, 'Track fetched!')
110111

111-
// If you initialized the SDK with your API secret, you can write data as well.
112-
// For example, to favorite the track above:
112+
// Favorite a track
113113
const userId = (
114114
await audiusSdk.users.getUserByHandle({
115115
handle: 'Your Audius handle goes here',
116116
})
117117
).data?.id
118-
const track = await audiusSdk.tracks.favoriteTrack({
118+
await audiusSdk.tracks.favoriteTrack({
119119
trackId: 'D7KyD',
120120
userId,
121121
})
@@ -128,7 +128,7 @@ import { sdk } from '@audius/sdk'
128128

129129
const audiusSdk = sdk({
130130
apiKey: 'Your API Key goes here',
131-
apiSecret: 'Your API Secret goes here',
131+
bearerToken: 'Your Bearer Token goes here',
132132
})
133133

134134
const track = await audiusSdk.tracks.getTrack({ trackId: 'D7KyD' })
@@ -140,44 +140,19 @@ const userId = (
140140
})
141141
).data?.id
142142

143-
const track = await audiusSdk.tracks.favoriteTrack({
143+
await audiusSdk.tracks.favoriteTrack({
144144
trackId: 'D7KyD',
145145
userId,
146146
})
147147
console.log('Track favorited!')
148148
```
149149
150-
:::note
151-
152-
Writing data (such as uploading or favoriting a track) is only possible if you provide an apiSecret
153-
154-
:::
155-
156-
:::note
157-
158-
If you are using the sdk in a browser environment you will need to do:
159-
160-
```js
161-
import Web3 from 'web3'
162-
window.Web3 = Web3
163-
```
164-
165-
:::
166-
167-
:::note
168-
169-
If your bundler doesn't automatically polyfill node libraries (like when using create-react-app v5)
170-
you will need to use the `web3` script tag instead of the `web3` npm package.
171-
172-
:::
173-
174150
## Full HTML + JS example
175151
176152
```html title="index.html" showLineNumbers
177153
<!DOCTYPE html>
178154
<html>
179155
<head>
180-
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
181156
<script src="https://cdn.jsdelivr.net/npm/@audius/sdk@latest/dist/sdk.min.js"></script>
182157
<script>
183158
const fn = async () => {
@@ -203,3 +178,23 @@ you will need to use the `web3` script tag instead of the `web3` npm package.
203178
accounts
204179
205180
- [Explore the API docs](/developers/sdk/tracks) to see what else you can do with the Audius SDK
181+
182+
## Direct API Access
183+
184+
You can also access the Audius API directly without the SDK:
185+
186+
**REST API:**
187+
188+
```bash
189+
curl -X GET "https://api.audius.co/v1/tracks/trending" \
190+
-H "Authorization: Bearer <YOUR-API-BEARER-TOKEN>"
191+
```
192+
193+
**gRPC:**
194+
195+
```bash
196+
grpcurl -H "authorization: Bearer <YOUR-API-BEARER-TOKEN>" \
197+
grpc.audius.co:443 list
198+
```
199+
200+
For more details, visit the [API documentation](https://docs.audius.co/api) or the [Swagger definition](https://api.audius.co/v1).

packages/common/src/api/tan-query/collection/useUpdateCollection.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { USDC } from '@audius/fixed-decimal'
21
import { Id } from '@audius/sdk'
32
import { useMutation, useQueryClient } from '@tanstack/react-query'
43
import { isEqual } from 'lodash'
@@ -9,7 +8,6 @@ import { fileToSdk } from '~/adapters/track'
98
import {
109
getCollectionQueryKey,
1110
getTrackQueryKey,
12-
useCurrentAccountUser,
1311
useCurrentUserId
1412
} from '~/api'
1513
import { useQueryContext } from '~/api/tan-query/utils'
@@ -40,13 +38,6 @@ export const useUpdateCollection = () => {
4038
imageUtils: { generatePlaylistArtwork }
4139
} = useQueryContext()
4240
const queryClient = useQueryClient()
43-
const { data: account } = useCurrentAccountUser({
44-
select: (user) => ({
45-
erc_wallet: user?.erc_wallet,
46-
wallet: user?.wallet
47-
})
48-
})
49-
const { erc_wallet, wallet } = account ?? {}
5041
const dispatch = useDispatch()
5142
const { data: currentUserId } = useCurrentUserId()
5243

@@ -86,30 +77,15 @@ export const useUpdateCollection = () => {
8677
const priceCents = Number(
8778
collectionUpdate.stream_conditions.usdc_purchase.price
8879
)
89-
const priceWei = Number(USDC(priceCents / 100).value.toString())
90-
91-
const walletToUse = erc_wallet ?? wallet
92-
if (!walletToUse) {
93-
throw new Error('No wallet found for user')
94-
}
95-
96-
// Get the user's USDC bank address from the wallet
97-
const { userBank } =
98-
await sdk.services.claimableTokensClient.getOrCreateUserBank({
99-
ethWallet: walletToUse,
100-
mint: 'USDC'
101-
})
102-
const userBankStr = userBank.toString()
10380

10481
// Update the stream conditions with the price and splits
10582
collectionUpdate.stream_conditions = {
10683
usdc_purchase: {
10784
price: priceCents,
10885
splits: [
10986
{
110-
payout_wallet: userBankStr,
111-
percentage: 100,
112-
amount: priceWei
87+
user_id: currentUserId,
88+
percentage: 100
11389
}
11490
]
11591
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * as confirmerActions from './actions'
22
export { default as confirmerReducer } from './reducer'
33
export * as confirmerSelectors from './selectors'
4-
export { default as confirmerSagas, confirmTransaction } from './sagas'
4+
export { default as confirmerSagas } from './sagas'
55
export * from './types'
66
export { initialState as initialConfirmerState } from './reducer'

packages/common/src/store/confirmer/sagas.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { call, delay, put, race, select, takeEvery } from 'redux-saga/effects'
22

33
import { waitForValue } from '~/utils/sagaHelpers'
44

5-
import { getSDK } from '..'
6-
75
import * as confirmerActions from './actions'
86
import {
97
getResult,
@@ -17,18 +15,6 @@ import {
1715
} from './selectors'
1816
import type { RequestConfirmationError } from './types'
1917

20-
export function* confirmTransaction(blockHash: string, blockNumber: number) {
21-
const sdk = yield* getSDK()
22-
yield call(
23-
[sdk.services.entityManager, sdk.services.entityManager.confirmWrite],
24-
{
25-
blockHash,
26-
blockNumber
27-
}
28-
)
29-
return true
30-
}
31-
3218
/* Private */
3319

3420
// Makes a call and races a confirmation callback against a timeout, enqueues requests with

0 commit comments

Comments
 (0)