Authentication Failed

I am Trying to get Users of Builder.io using https://cdn.builder.io/api/v1/users?apiKey=“{my_public_key}”&cachebust=true , But There is no documentation for How to Authenticate using token ,Can Someone please help?

Responce:401 Authentication Required

Hi @aditi Welcome to Builder.io Forum!

Fetching users from Builder.io using the API requires proper authentication, and typically the endpoints that allow you to access user information may need an authentication token rather than the public API key alone. To securely access user data, you will likely need to use the Builder.io Admin API with authentication.

Steps to Authenticate and Fetch Users

  1. Generate an Admin API Key
  2. Use the Admin API Endpoint with Proper Authentication Header

Step 1: Generate an Admin API Key

  1. Log in to Your Builder.io Account:

    • Navigate to Builder.io and log in to your account.
  2. Access API Settings:

    • Go to Account Settings or Space Settings.
    • Find the section for API Keys.
    • Generate a new Private/Admin API Key if you don’t already have one.

Step 2: Use the Admin API Endpoint with Proper Authentication Header

With the Admin API key, you can access secure endpoints. Here is an example of how to do this using a tool like curl or a compatible HTTP client in your language of choice.

Using Curl:

curl -X GET "https://api.builder.io/v1/admin/users" \
  -H "Authorization: Bearer YOUR_ADMIN_API_KEY"

Replace YOUR_ADMIN_API_KEY with your actual Admin API key.

Using Fetch in JavaScript:

const fetchUsers = async () => {
  const response = await fetch('https://api.builder.io/v1/admin/users', {
    method: 'GET',
    headers: {
      'Authorization': `Bearer YOUR_ADMIN_API_KEY`,
      'Content-Type': 'application/json'
    }
  });
  
  if (!response.ok) {
    throw new Error('Network response was not ok ' + response.statusText);
  }
  const data = await response.json();
  console.log(data);
};

fetchUsers().catch(console.error);

Example of Fetching Users in Node.js:

const fetch = require('node-fetch');

const fetchUsers = async () => {
  try {
    const response = await fetch('https://api.builder.io/v1/admin/users', {
      method: 'GET',
      headers: {
        'Authorization': `Bearer YOUR_ADMIN_API_KEY`,
        'Content-Type': 'application/json',
      }
    });

    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }

    const users = await response.json();
    console.log(users);
  } catch (error) {
    console.error('Error fetching users:', error);
  }
};

fetchUsers();

Verifying Permissions and Access

Ensure that the Admin API key has the required permissions to access user data. Double-check the settings and permissions for the API key in your Builder.io account.

Troubleshooting Common Issues

  1. Invalid API Key: Ensure you are using the correct admin API key and not the public API key.
  2. Permission Denied: Verify that the API key has the necessary permissions.
  3. Endpoint URL: Ensure you are hitting the correct endpoint for the user data.

Documentation Links

Conclusion

By following these steps, you can securely fetch user information from Builder.io using the Admin API key. Ensure proper permissions and correct endpoint usage to avoid authentication errors such as the 401 status.