Users - API Key Management
  • 22 Dec 2023
  • 2 Minutes to read
  • Dark
    Light

Users - API Key Management

  • Dark
    Light

Article summary

Users can now generate an API key that can be used instead of a username + password combination.

You can either create a new API key, delete an existing API key, retrieve details like last_auth time, key_id of a single API key, or list all API keys

Create a new API Token

POST /1/users/{user_id}/apiKeys
Host: https://api.cogniac.io

Example to create a new API token

curl -X POST https://api.cogniac.io/1/users/di71rG94/apiKeys \
-H "Authorization: Bearer abcdefg.hijklmnop.qrstuvwxyz" \
-H "Content-Type: application/json" \
-d '{
  "description":"New Token for Image downloads"
}' | json_pp

import requests
import json
from pprint import pprint

url_prefix = 'https://api.cogniac.io'
api_version = "1"
user_id = "di71rG94"
token = ''  # add your token here
headers = {'Authorization': f'Bearer {token}',
           'Content-Type': 'application/json'}
user_data = {
  "description":"New Token for Image downloads"
}

url = f'{url_prefix}/{api_version}/users/{user_id}/apiKeys'
res = requests.post(url, json=user_data, headers=headers)

if res.status_code == 200:
    response_data = json.loads(res.content)
    pprint(response_data)
else:
    print(f"Error: {res.status_code}, {res.text},{res.headers}")

{
  "user_id": "ufL2GnI3VDgV",
  "description": "New API token for image downloads",
  "created_at": 1550011606, 
  "key_id":"LZIDR",
  "api_key":"LZIDR:H00FQKIJZHEIAB5TJGJXL94UIDW4",
  "email": "xyz@cogniac.co"
}

Note: API token

API tokens can only be accessed once, at the time of creation. Please save the API token at the time of creation. You cannot retrieve the token later using the GET call

Delete API token

DELETE /1/users/{user_id}/apiKeys/{api_key_id}
Host: https://api.cogniac.io
curl -X DELETE https://api.cogniac.io/1/users/ufL2GnI3VDgV/apiKeys/LZIDR \
-H "Authorization: Bearer abcdefg.hijklmnop.qrstuvwxyz" \
| json_pp
import requests

url_prefix = 'https://api.cogniac.io'
api_version = "1"
user_id = "ufL2GnI3VDgV"
api_key_id = "LZIDR"
token = ''  # add your token here
headers = {'Authorization': f'Bearer {token}',
           'Content-Type': 'application/json'}

url = f'{url_prefix}/{api_version}/users/{user_id}/apiKeys/{api_key_id}'
res = requests.delete(url, headers=headers)

if res.status_code == 204:
    print(api_key_id + " was successfully deleted.")
else:
    print(f"Error: {res.status_code}, {res.text}")

HTTP 204 Code (with no body)

Retrieve individual API token

GET /1/users/{user_id}/apiKeys/{api_key_id}
Host: https://api.cogniac.io

import requests
import json
from pprint import pprint

url_prefix = 'https://api.cogniac.io'
api_version = "1"
user_id = "ufL2GnI3VDgV"
api_key_id = "LZIDR"
token = 'abcdefg.hijklmnop.qrstuvwxyz'  # add your token here
headers = {'Authorization': f'Bearer {token}',
           'Content-Type': 'application/json'}  # Fixed the headers


url = f'{url_prefix}/{api_version}/users/{user_id}/apiKeys/{api_key_id}'
res = requests.get(url, headers=headers)

if res.status_code == 200:
    response_data = json.loads(res.content)
    pprint(response_data)
else:
    print(f"Error: {res.status_code}, {res.text}")

curl -X GET https://api.cogniac.io/1/users/di71rG94/apiKeys/g56gdercb \
-H "Authorization: Bearer abcdefg.hijklmnop.qrstuvwxyz"
{
  "user_id": "ufL2GnI3VDgV",
  "description": "New API token for image downloads",
  "created_at": 1550011606, 
  "key_id":"LZIDR",
  "api_key":None,
  "last_auth" : None,
  "email": "xyz@cogniac.co"
}

List all API tokens

GET /1/users/{user_id}/apiKeys
Host: https://api.cogniac.io
curl -X GET https://api.cogniac.io/1/users/ufL2GnI3VDgV/apiKeys \
-H "Authorization: Bearer abcdefg.hijklmnop.qrstuvwxyz" \

import requests
import json
from pprint import pprint

url_prefix = 'https://api.cogniac.io'
api_version = "1"
user_id = "ufL2GnI3VDgV"
token = 'abcdefg.hijklmnop.qrstuvwxyz'  # add your token here
headers = {'Authorization': f'Bearer {token}',
           'Content-Type': 'application/json'}  # Fixed the headers


url = f'{url_prefix}/{api_version}/users/{user_id}/apiKeys'
res = requests.get(url, headers=headers)

if res.status_code == 200:
    response_data = json.loads(res.content)
    pprint(response_data)
else:
    print(f"Error: {res.status_code}, {res.text}")

{
    "data": [
      {
  "user_id": "ufL2GnI3VDgV",
  "description": "New API token for image downloads",
  "created_at": 1550011606, 
  "key_id":"LZIDR",
  "api_key":None,
  "last_auth" : None,
  "email": "xyz@cogniac.co"
      }]
}


Was this article helpful?

What's Next
Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.
ESC

Eddy AI, facilitating knowledge discovery through conversational intelligence