Authentication overview

Block's developer platform, primarily accessed through the Square API, utilizes robust authentication mechanisms to secure interactions between applications and Block's services. These mechanisms ensure that only authorized applications and users can perform operations such as processing payments, managing inventory, or accessing customer data. The core principle revolves around verifying the identity of the requesting entity and granting appropriate permissions based on established credentials. For developers, this means understanding how to obtain, manage, and use secure tokens to authorize API calls.

Block's authentication framework is built upon industry-standard protocols, emphasizing security, scalability, and ease of integration. It supports various use cases, from single-purpose internal tools to complex third-party applications serving a wide user base. Proper authentication is critical not only for security but also for maintaining compliance with regulations like PCI DSS Level 1 for payment processing, which Block adheres to for its Square product ecosystem.

Supported authentication methods

Block supports several authentication methods tailored to different application types and security requirements. The primary methods are OAuth 2.0 and Personal Access Tokens. Choosing the correct method depends on whether your application acts on behalf of other users (requiring delegated authorization) or as a direct integration for a specific developer account.

Method When to Use Security Level
OAuth 2.0 Third-party applications, public applications, applications needing delegated access to seller accounts. High. Securely delegates authorization without sharing user credentials. Leverages access and refresh tokens.
Personal Access Tokens Private applications, internal tools, scripts for a single developer account, rapid prototyping. Moderate to High. Provides direct, limited access. Requires careful management to prevent exposure.

OAuth 2.0

OAuth 2.0 is the recommended authentication method for most Block applications, especially those that interact with multiple seller accounts or act on behalf of users. It is an authorization framework that enables an application to obtain limited access to a user's protected resources without exposing the user's credentials to the application. The OAuth 2.0 specification outlines various grant types, and Block primarily uses the Authorization Code grant flow, which is suitable for web applications and mobile apps.

The OAuth flow involves:

  1. The application redirects the user to Block's authorization page.
  2. The user grants the application permission to access their Block account data.
  3. Block redirects the user back to the application with an authorization code.
  4. The application exchanges the authorization code for an access_token and a refresh_token by making a server-side request to Block's token endpoint.

The access_token is a short-lived credential used to authorize API requests, while the refresh_token is used to obtain new access_tokens once the current one expires, without requiring the user to re-authorize the application.

Personal Access Tokens

Personal Access Tokens (PATs) provide a simpler authentication mechanism for applications that only need to access data for the developer's own Block account. These tokens are generated directly from the Block Developer Dashboard and provide direct access to specific API permissions. PATs are appropriate for backend services, command-line tools, or scripts where a user interface for OAuth redirection is not practical or necessary.

Unlike OAuth tokens, PATs do not expire automatically, making them convenient for long-running processes. However, this also means they require stringent security practices for storage and rotation, as their compromise could lead to unauthorized access to your Block account.

Getting your credentials

To begin authenticating with Block's APIs, you need to set up your application and obtain the necessary credentials through the Block Developer Dashboard. This process varies slightly depending on whether you are using OAuth 2.0 or Personal Access Tokens.

For OAuth 2.0 applications:

  1. Create an Application: Log in to the Block Developer Dashboard and create a new application.
  2. Obtain Application ID and Secret: The dashboard will provide an Application ID and an Application Secret. The Application ID is public and identifies your application, while the Application Secret is a confidential key used to secure token exchange.
  3. Configure Redirect URL: Specify one or more OAuth Redirect URLs in your application settings. These are the URLs to which Block will redirect users after they grant your application permissions.
  4. Define Permissions (Scopes): Select the API permissions (scopes) your application requires. These determine what data and actions your application can access on behalf of a seller. For example, PAYMENTS_WRITE to process payments or CUSTOMERS_READ to view customer profiles. Careful selection of Block API permissions is crucial for security.

For Personal Access Tokens:

  1. Access Developer Dashboard: Log in to the Block Developer Dashboard.
  2. Generate Token: Navigate to the 'Credentials' section for your application and choose to generate a new Personal Access Token.
  3. Select Permissions: Assign the specific API permissions that the token should have. Granular permission control helps limit the impact if a token is compromised.
  4. Store Token Securely: The token will be displayed once. Copy it immediately and store it securely, as it will not be retrievable from the dashboard again.

Authenticated request example

Once you have obtained an access token (either OAuth 2.0 or Personal Access Token), you can use it to authorize your API requests. Block's APIs typically expect the access token to be included in the Authorization header of your HTTP requests, using the Bearer scheme.

Here's an example of an authenticated request using Python:


import http.client
import json

# Replace with your actual access token
ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"

# Replace with your actual Square Application ID
APPLICATION_ID = "YOUR_APPLICATION_ID"

# API endpoint for listing locations
HOST = "connect.squareup.com"
PATH = "/v2/locations"

conn = http.client.HTTPSConnection(HOST)

headers = {
    'Square-Version': '2024-05-29', # Use the current API version
    'Authorization': f'Bearer {ACCESS_TOKEN}',
    'Content-Type': 'application/json',
    'Accept': 'application/json'
}

conn.request("GET", PATH, headers=headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

This example demonstrates how to set the Authorization header with the Bearer token. The Square-Version header is also important to specify the API version your application targets, ensuring consistent behavior. Block's API versioning policy often requires this header.

Security best practices

Implementing strong security practices is paramount when working with Block's authentication credentials to protect sensitive financial and customer data. Adhering to these guidelines helps prevent unauthorized access and ensures the integrity of your integrations.

  • Never hardcode credentials: Avoid embedding access tokens or application secrets directly in your source code. Use environment variables, secure configuration files, or secret management services.
  • Store credentials securely: Access tokens and refresh tokens, especially Personal Access Tokens, should be stored in secure, encrypted storage. Implement proper access controls to limit who can retrieve these tokens.
  • Use HTTPS for all communications: All communication with Block's APIs must occur over HTTPS to encrypt data in transit and prevent eavesdropping. This is a fundamental requirement for OAuth 2.0 security and good practice for any API integration.
  • Implement OAuth Refresh Tokens: For OAuth integrations, use refresh tokens to obtain new access tokens. This minimizes the exposure window of any single access token and improves application resilience against token expiration. Properly manage refresh token rotation if Block's policy requires it.
  • Restrict API Permissions (Scopes): Always request the minimum necessary permissions (scopes) for your application. Granting excessive permissions increases the risk if your credentials are compromised. Review and update permissions regularly.
  • Rotate Personal Access Tokens: While PATs do not expire, it is a recommended security practice to rotate them periodically. Delete old tokens and generate new ones to reduce the risk of long-term exposure.
  • Implement input validation and error handling: Validate all input from users and API responses to prevent common vulnerabilities like injection attacks. Implement robust error handling to gracefully manage failed authentications or expired tokens.
  • Monitor logs and alerts: Regularly review API access logs for unusual activity or unauthorized attempts. Set up alerts for authentication failures or suspicious API calls to detect potential security incidents promptly.