🔐 API Authentication

Learn how to authenticate your API requests using OAuth 2.0 and API keys

🌟 Overview

All Sports Buddies API requests require authentication to ensure security and proper access control. We support two main authentication methods:

💡 Best Practice

Use OAuth 2.0 for applications that interact with user data, and API keys for backend services and integrations.

🔄 OAuth 2.0 Flow

OAuth 2.0 is the recommended authentication method for applications that need to access user data on behalf of Sports Buddies users.

Authorization Code Flow

This is the most secure OAuth flow and is recommended for web applications:

  1. Redirect User: Redirect users to our authorization endpoint
  2. User Authorization: User grants permission to your application
  3. Authorization Code: We redirect back with an authorization code
  4. Exchange for Tokens: Exchange the code for access and refresh tokens
  5. Make API Calls: Use the access token for API requests

Step 1: Redirect to Authorization Endpoint

https://api.joinsportsbuddies.app/oauth/authorize? client_id=YOUR_CLIENT_ID& redirect_uri=YOUR_REDIRECT_URI& response_type=code& scope=read write& state=RANDOM_STATE_STRING

Step 2: Exchange Authorization Code for Tokens

POST https://api.joinsportsbuddies.app/oauth/token Content-Type: application/x-www-form-urlencoded grant_type=authorization_code& client_id=YOUR_CLIENT_ID& client_secret=YOUR_CLIENT_SECRET& code=AUTHORIZATION_CODE& redirect_uri=YOUR_REDIRECT_URI

Step 3: Token Response

{ "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "def50200...", "scope": "read write" }

🔑 API Keys

API keys are suitable for server-to-server integrations and applications that don't require user-specific data access.

⚠️ Security Warning

Keep your API keys secure and never expose them in client-side code or public repositories. API keys should be treated like passwords.

Using API Keys

Include your API key in the Authorization header of your requests:

Authorization: Bearer YOUR_API_KEY Content-Type: application/json

Request Example

curl -X GET "https://api.joinsportsbuddies.app/v1/users/me" \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json"

🎯 OAuth Scopes

Scopes define the level of access your application has to user data and platform features.

Scope Description Access Level
read Read access to user profile and public data Basic
write Create and update user data, send messages Standard
profile Full access to user profile information Enhanced
matching Access to matching algorithm and recommendations Enhanced
events Create and manage events and activities Enhanced
admin Administrative access (enterprise only) Enterprise

🔄 Token Management

Proper token management is crucial for maintaining secure API access.

Access Token Expiration

Access tokens expire after 1 hour (3600 seconds) for security reasons. You should:

Refreshing Access Tokens

POST https://api.joinsportsbuddies.app/oauth/token Content-Type: application/x-www-form-urlencoded grant_type=refresh_token& client_id=YOUR_CLIENT_ID& client_secret=YOUR_CLIENT_SECRET& refresh_token=YOUR_REFRESH_TOKEN

Token Refresh Response

{ "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "def50200...", "scope": "read write" }

❌ Error Handling

Proper error handling ensures your application can respond appropriately to authentication failures.

Common Authentication Errors

Error Code Description Solution
invalid_client Invalid client ID or secret Check your credentials
invalid_grant Invalid authorization code or refresh token Request new authorization
invalid_scope Requested scope is invalid Check scope values
unauthorized_client Client not authorized for requested grant type Contact support
access_denied User denied authorization Handle gracefully

Error Response Format

{ "error": "invalid_grant", "error_description": "The authorization code has expired", "error_uri": "https://docs.joinsportsbuddies.app/api/errors" }

🛡️ Security Best Practices

Follow these security guidelines to protect your application and users.

Token Security

Client Security

User Privacy

💻 Implementation Examples

Here are complete examples for different programming languages and platforms.

JavaScript (Node.js)

const axios = require('axios'); class SportsBuddiesAPI { constructor(clientId, clientSecret) { this.clientId = clientId; this.clientSecret = clientSecret; this.baseURL = 'https://api.joinsportsbuddies.app'; } async getAccessToken(authorizationCode, redirectUri) { const response = await axios.post(`${this.baseURL}/oauth/token`, { grant_type: 'authorization_code', client_id: this.clientId, client_secret: this.clientSecret, code: authorizationCode, redirect_uri: redirectUri }); return response.data.access_token; } async makeAuthenticatedRequest(endpoint, accessToken) { const response = await axios.get(`${this.baseURL}${endpoint}`, { headers: { 'Authorization': `Bearer ${accessToken}`, 'Content-Type': 'application/json' } }); return response.data; } } // Usage const api = new SportsBuddiesAPI('your_client_id', 'your_client_secret');

Python

import requests class SportsBuddiesAPI: def __init__(self, client_id, client_secret): self.client_id = client_id self.client_secret = client_secret self.base_url = 'https://api.joinsportsbuddies.app' def get_access_token(self, authorization_code, redirect_uri): response = requests.post(f'{self.base_url}/oauth/token', data={ 'grant_type': 'authorization_code', 'client_id': self.client_id, 'client_secret': self.client_secret, 'code': authorization_code, 'redirect_uri': redirect_uri }) return response.json()['access_token'] def make_authenticated_request(self, endpoint, access_token): headers = { 'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json' } response = requests.get(f'{self.base_url}{endpoint}', headers=headers) return response.json() # Usage api = SportsBuddiesAPI('your_client_id', 'your_client_secret')

🧪 Testing & Development

Tools and tips for testing your authentication implementation.

Test Credentials

For development and testing, you can use our sandbox environment:

Testing Tools

🆘 Support & Resources

Get help with authentication and access additional resources.

📚 Additional Resources

Getting Help

Explore More Documentation

Discover other helpful guides and resources to get the most out of Sports Buddies.

🎯 Getting Started

Learn the basics of Sports Buddies, create your profile, and make your first connection.

📱 User Guides

Master all the features and learn how to get the most out of Sports Buddies.

🛡️ Safety & Trust

Learn about our PlaySafe™ features, verification process, and how we protect our community.

🤝 Community

Understand our community guidelines, moderation policies, and how to be a great community member.

⚙️ API & Developers

Technical documentation for developers, integrations, and API access.

❓ Help & Support

Find answers to common questions and get help when you need it.

📋 Policies

Read our terms of service, privacy policy, and other important legal documents.