Skip to main content
This tutorial was written by Claude Code (an AI) and has not yet been reviewed. Follow along with caution. If the tutorial was helpful or a specific part was not clear/correct, please provide feedback at the bottom of the page. Thank you.
Consumer keys are required for administrative operations using the CoCart Sessions API. This guide covers setup and best practices for server-side authentication.

Overview

The CoCart Sessions API requires WooCommerce REST API authentication using consumer keys. This API is designed for administrative and server-side operations such as viewing all cart sessions, managing customer carts, and debugging session data.
Important: Consumer keys are NOT needed for the Checkout API. Guest users and customers interact with the Checkout API using cart keys (session-based authentication). Only use consumer keys for Sessions API operations.

What is the Sessions API?

The Sessions API allows you to:

View All Cart Sessions

Retrieve a paginated list of all active cart sessions in your store

Get Session Details

View specific cart session data including items, customer info, and totals

Delete Sessions

Remove abandoned or invalid cart sessions programmatically

Debug Customer Issues

Troubleshoot cart problems by inspecting session data and expiration

Why Consumer Keys for Sessions API?

Administrative Access

Prevents unauthorized access to sensitive cart session data across all users

Server-to-Server Auth

Secure authentication for backend systems and integrations

Audit Trail

Track which applications are accessing session management endpoints

Permission Control

Read or Read/Write permissions for different operational needs

Step 1: Generate Consumer Keys

Via WordPress Admin

  1. Navigate to WooCommerce Settings
  2. Click “Add Key”
  3. Configure Key Settings
    • Description: Enter a meaningful name (e.g., “Sessions API - Admin Dashboard”)
    • User: Select a user account (typically an admin)
    • Permissions: Choose appropriate level:
    • Read - For viewing sessions only (GET requests)
    • Write - For deleting sessions (DELETE requests)
    • Read/Write - Full access (recommended for session management)
  4. Generate and Save Keys
    • Consumer Key: Starts with ck_ (public identifier)
    • Consumer Secret: Starts with cs_ (private secret)
Copy and securely store both keys immediately. The consumer secret cannot be retrieved again after this screen.

Via WP-CLI

Programmatically (PHP)

Step 2: Configure API Keys for Different Environments

Development Environment

Production Environment

Environment Variables (.env)

Step 3: Implement Sessions API Authentication

Basic Implementation (JavaScript/Node.js)

PHP Implementation

Step 4: Common Sessions API Use Cases

Monitor Active Carts

Debug Customer Cart Issues

Clean Up Abandoned Carts

Build Admin Dashboard

Step 5: Security Best Practices

Key Storage for Server-Side Applications

Critical: Consumer keys for Sessions API should ONLY be used in server-side applications. Never expose these keys to client-side code or browsers.
Store in WordPress options with proper access controls
Use cloud secrets managers for production

Key Rotation

Step 6: Monitor and Manage API Keys

Key Usage Monitoring

Automated Key Management

Troubleshooting

Common Issues

401 Unauthorized

Causes:
  • Invalid consumer key/secret
  • Incorrect Basic Auth header format
  • Key disabled or revoked
Solutions:
  • Verify Base64 encoding: base64(key:secret)
  • Check key exists in WooCommerce → Settings → Advanced → REST API
  • Ensure key has not been deleted

403 Forbidden

Causes:
  • Read-only key used for DELETE operations
  • User associated with key lacks permissions
  • IP restrictions or firewall rules
Solutions:
  • Use Read/Write key for delete operations
  • Ensure key user has manage_woocommerce capability
  • Check server firewall settings

404 Not Found

Causes:
  • Incorrect API endpoint URL
  • Session key doesn’t exist
  • Pretty permalinks not enabled
Solutions:
  • Verify endpoint: /wp-json/cocart/v2/sessions
  • Check cart key is valid and exists
  • Enable permalinks in WordPress Settings

Empty Response

Causes:
  • No active cart sessions
  • Pagination exceeds available data
  • Sessions expired and cleaned up
Solutions:
  • Check if any carts exist in store
  • Reduce per_page or page parameter
  • Verify session expiration settings

Key Exposure Risk

Causes:
  • Keys in version control (.git)
  • Keys in client-side code
  • Keys in logs or error messages
Solutions:
  • Add .env to .gitignore
  • Only use keys server-side
  • Sanitize logs before debugging
  • Rotate keys immediately if exposed

Rate Limiting

Causes:
  • Too many requests in short timeframe
  • Shared keys across multiple systems
Solutions:
  • Implement exponential backoff
  • Use separate keys per application
  • Cache session data when possible

Debug Mode

Testing Authentication

Example Success Response:
Example Error Response:

Integration Examples

WordPress Admin Dashboard Plugin

Node.js Backend Service

Python Analytics Script

Security Reminder: All these examples are server-side only. Never expose consumer keys in client-side code, frontend applications, or public repositories.