Skip to content

Commit

Permalink
update secret docs
Browse files Browse the repository at this point in the history
  • Loading branch information
oceans404 committed Nov 16, 2024
1 parent cf01c39 commit bebd01c
Showing 1 changed file with 211 additions and 3 deletions.
214 changes: 211 additions & 3 deletions docs/storage-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,203 @@ print('Second secret retrieved:', secret2)
</TabItem>
</Tabs>

### 6. Update Secret

Update an existing secret's value using PUT `/api/apps/{app_id}/secrets/{store_id}` with the secret's store ID and updated value.

**Make sure to update the `APP_ID` and `STORE_ID` below with your values before running the code:**

<Tabs>
<TabItem value="js" label="JavaScript">

```javascript
const APP_ID = 'INSERT_YOUR_APP_ID_HERE';
const STORE_ID = 'INSERT_YOUR_STORE_ID_HERE'; // use the store_id of the secret you want to update
const USER_SEED = 'user_123'; // generates a deterministic nillion user id; use any string
const API_BASE = 'https://nillion-storage-apis-v0.onrender.com';

// Update the secret with a new value
console.log('\nUpdating secret...');
const updateResponse = await fetch(
`${API_BASE}/api/apps/${APP_ID}/secrets/${STORE_ID}`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
nillion_seed: USER_SEED,
secret_value: "gm gm gm gm!",
secret_name: "my_secret" // use the original secret name
}),
}
).then((res) => res.json());
console.log('Secret updated:', updateResponse);

// Verify the updated secret
const verifyResponse = await fetch(
`${API_BASE}/api/secret/retrieve/${STORE_ID}?retrieve_as_nillion_user_seed=${USER_SEED}&secret_name=my_secret`
).then((res) => res.json());
console.log('Updated secret retrieved:', verifyResponse);
```

</TabItem>
<TabItem value="Python" label="Python">

```python
import requests

APP_ID = 'INSERT_YOUR_APP_ID_HERE'
STORE_ID = 'INSERT_YOUR_STORE_ID_HERE' # use the store_id of the secret you want to update
USER_SEED = 'user_123' # generates a deterministic nillion user id; use any string
API_BASE = 'https://nillion-storage-apis-v0.onrender.com'

print('\nUpdating secret...')
updateResponse = requests.put(
f'{API_BASE}/api/apps/{APP_ID}/secrets/{STORE_ID}',
headers={'Content-Type': 'application/json'},
json={
'nillion_seed': USER_SEED,
'secret_value': "gm gm gm gm!",
'secret_name': "my_secret" # use the original secret name
}
).json()
print('Secret updated:', updateResponse)

# Verify the updated secret
verifyResponse = requests.get(
f'{API_BASE}/api/secret/retrieve/{STORE_ID}',
params={
'retrieve_as_nillion_user_seed': USER_SEED,
'secret_name': 'my_secret'
}
).json()
print('Updated secret retrieved:', verifyResponse)
```

</TabItem>
</Tabs>

## Quickstart Complete Code

**Make sure to update the `APP_ID` below with your app_id from step 0 before running the code:**

<Tabs>
<TabItem value="js" label="JavaScript">
```javascript reference showGithubLink
https://github.com/oceans404/nillion-storage-apis-v0/blob/main/Quickstart.md?plain=1#L48-L131
```javascript
const APP_ID = 'INSERT_YOUR_APP_ID_HERE';
const USER_SEED = 'user_123'; // your seed generates a deterministic nillion user id - check it in step 1
const API_BASE = 'https://nillion-storage-apis-v0.onrender.com';

async function runQuickstart() {
try {
// 1. Check your deterministic user ID
console.log('Checking user ID generated by your seed...');
const user = await fetch(`${API_BASE}/api/user`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
nillion_seed: USER_SEED,
}),
}).then((res) => res.json());
console.log('Your User ID:', user.nillion_user_id);

// 2. Store first secret (number)
console.log('\nStoring first secret...');
const storeResult1 = await fetch(`${API_BASE}/api/apps/${APP_ID}/secrets`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
secret: {
nillion_seed: USER_SEED,
secret_value: 16,
secret_name: 'my_secret_number',
},
permissions: {
retrieve: [],
update: [],
delete: [],
compute: {},
},
}),
}).then((res) => res.json());
console.log('First secret stored at:', storeResult1);

// 3. Store second secret (string/blob)
console.log('\nStoring second secret...');
const storeResult2 = await fetch(`${API_BASE}/api/apps/${APP_ID}/secrets`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
secret: {
nillion_seed: USER_SEED,
secret_value: 'gm!',
secret_name: 'my_secret_blob',
},
permissions: {
retrieve: [],
update: [],
delete: [],
compute: {},
},
}),
}).then((res) => res.json());
console.log('Second secret stored at:', storeResult2);

// 4. List store IDs
console.log('\nListing store IDs...');
const storeIds = await fetch(`${API_BASE}/api/apps/${APP_ID}/store_ids`)
.then((res) => res.json())
.then((data) => data.store_ids);
console.log('Store IDs:', storeIds);

// 5. Retrieve both secrets using the store IDs we just created
console.log('\nRetrieving secrets...');
const secret1 = await fetch(
`${API_BASE}/api/secret/retrieve/${storeIds[0].store_id}?retrieve_as_nillion_user_seed=${USER_SEED}&secret_name=${storeIds[0].secret_name}`
).then((res) => res.json());
console.log('First secret retrieved:', secret1);

const secret2 = await fetch(
`${API_BASE}/api/secret/retrieve/${storeIds[1].store_id}?retrieve_as_nillion_user_seed=${USER_SEED}&secret_name=${storeIds[1].secret_name}`
).then((res) => res.json());
console.log('Second secret retrieved:', secret2);

// 6. Update first secret
console.log('\nUpdating first secret...');
const updateResponse = await fetch(
`${API_BASE}/api/apps/${APP_ID}/secrets/${storeIds[0].store_id}`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
nillion_seed: USER_SEED,
secret_value: "gm gm gm gm!",
secret_name: storeIds[0].secret_name
}),
}
).then((res) => res.json());
console.log('Secret updated:', updateResponse);

// Verify the update
const verifyUpdate = await fetch(
`${API_BASE}/api/secret/retrieve/${storeIds[0].store_id}?retrieve_as_nillion_user_seed=${USER_SEED}&secret_name=${storeIds[0].secret_name}`
).then((res) => res.json());
console.log('Updated secret retrieved:', verifyUpdate);

} catch (error) {
console.error('Error in flow:', error);
}
}

// Run the quickstart
runQuickstart();
```
</TabItem>
<TabItem value="Python" label="Python">
```
```python
import requests

APP_ID = 'INSERT_YOUR_APP_ID_HERE'
Expand Down Expand Up @@ -443,6 +628,29 @@ def run_quickstart():
).json()
print('Second secret retrieved:', secret2)

# 6. Update first secret
print('\nUpdating first secret...')
updateResponse = requests.put(
f'{API_BASE}/api/apps/{APP_ID}/secrets/{storeIds[0]["store_id"]}',
headers={'Content-Type': 'application/json'},
json={
'nillion_seed': USER_SEED,
'secret_value': "gm gm gm gm!",
'secret_name': storeIds[0]['secret_name']
}
).json()
print('Secret updated:', updateResponse)

# Verify the update
verifyUpdate = requests.get(
f'{API_BASE}/api/secret/retrieve/{storeIds[0]["store_id"]}',
params={
'retrieve_as_nillion_user_seed': USER_SEED,
'secret_name': storeIds[0]['secret_name']
}
).json()
print('Updated secret retrieved:', verifyUpdate)

except Exception as error:
print('Error in flow:', error)

Expand Down

0 comments on commit bebd01c

Please sign in to comment.