Index of every page on this site: llms.txt
All skills
grasmash avatar

/drupal-api-endpoints

@19514f4
Run it yourself

Patterns for custom Drupal API endpoints that use UUIDs instead of sequential IDs: route, controller, and fetch templates, response-shape contracts for installed app clients, webhook handlers, authenticated cURL testing, and the JSON:API filter-access cache-poisoning fix. Use when creating custom routes, AJAX or JSON endpoints for a frontend or mobile app, webhook receivers, or JSON:API filters, and before adding an endpoint, to check whether an existing one already serves the need.

Use this Skill: https://skilld.dev/gh/grasmash/drupal-claude-skills/drupal-api-endpoints

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesauthenticated-curl-examples.md

≈1.5k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Authenticated cURL Request Examples

This document provides examples of making authenticated HTTP requests to custom Drupal API endpoints using cURL, including how to obtain and use session tokens.

Getting a Session Token

To make authenticated requests, you first need to obtain a session token by logging in:

# Login and capture session cookie
curl -c /tmp/cookies.txt \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"name":"username","pass":"password"}' \
  https://<project>.ddev.site/user/login?_format=json

Or extract just the session name and value:

# Get session token (returns session_name=session_value)
SESSION=$(curl -s -c - \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"name":"username","pass":"password"}' \
  https://<project>.ddev.site/user/login?_format=json \
  | grep -oP 'SSESS\w+\t\K\w+')

SESSION_NAME=$(curl -s -c - \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"name":"username","pass":"password"}' \
  https://<project>.ddev.site/user/login?_format=json \
  | grep -oP 'SSESS\w+')

echo "Cookie: $SESSION_NAME=$SESSION"

Using Drush for One-Time Login

For testing, the easiest way to get an authenticated session is with drush:

# Generate a one-time login link
ddev drush uli

# Use the link in a browser to authenticate, then copy cookies from browser dev tools

Authenticated GET Request

# Using cookie file
curl -b /tmp/cookies.txt \
  -H "Content-Type: application/json" \
  https://<project>.ddev.site/my-module-api/status

# Using explicit cookie
curl -H "Cookie: SSESS1234567890abcdef=xyz123abc456" \
  -H "Content-Type: application/json" \
  https://<project>.ddev.site/my-module-api/status

Authenticated POST Request

# Example: trigger a POST action
curl -b /tmp/cookies.txt \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{"entity_uuid":"a1b2c3d4-e5f6-7890-abcd-ef1234567890"}' \
  https://<project>.ddev.site/my-module-api/join

# Example: trigger another POST action
curl -b /tmp/cookies.txt \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{}' \
  https://<project>.ddev.site/my-module-api/a1b2c3d4-e5f6-7890-abcd-ef1234567890/action

# Example: trigger a third POST action
curl -b /tmp/cookies.txt \
  -X POST \
  -H "Content-Type: application/json" \
  https://<project>.ddev.site/my-module-api/status/123/accept

Using CSRF Token for POST/PUT/DELETE

Some endpoints may require a CSRF token for state-changing operations:

# Get CSRF token
CSRF_TOKEN=$(curl -b /tmp/cookies.txt \
  https://<project>.ddev.site/session/token)

# Use CSRF token in request
curl -b /tmp/cookies.txt \
  -X POST \
  -H "Content-Type: application/json" \
  -H "X-CSRF-Token: $CSRF_TOKEN" \
  -d '{"field":"value"}' \
  https://<project>.ddev.site/some-api/endpoint

Testing with JSON:API

For JSON:API endpoints (under /jsonapi/), use similar patterns:

# Get current user
curl -b /tmp/cookies.txt \
  -H "Content-Type: application/vnd.api+json" \
  -H "Accept: application/vnd.api+json" \
  https://<project>.ddev.site/jsonapi/user/user?filter[drupal_internal__uid]=1

# PATCH a node
curl -b /tmp/cookies.txt \
  -X PATCH \
  -H "Content-Type: application/vnd.api+json" \
  -H "Accept: application/vnd.api+json" \
  -H "X-CSRF-Token: $CSRF_TOKEN" \
  -d '{"data":{"type":"node--article","id":"UUID","attributes":{"title":"New Title"}}}' \
  https://<project>.ddev.site/jsonapi/node/article/UUID

Complete Workflow Example

#!/bin/bash

# Configuration
BASE_URL="https://<project>.ddev.site"
USERNAME="testuser"
PASSWORD="testpass"
COOKIES="/tmp/cookies.txt"

# Step 1: Login and save cookies
echo "Logging in..."
curl -s -c "$COOKIES" \
  -X POST \
  -H "Content-Type: application/json" \
  -d "{\"name\":\"$USERNAME\",\"pass\":\"$PASSWORD\"}" \
  "$BASE_URL/user/login?_format=json" > /dev/null

# Step 2: Get CSRF token
echo "Getting CSRF token..."
CSRF_TOKEN=$(curl -s -b "$COOKIES" "$BASE_URL/session/token")

# Step 3: Make authenticated request
echo "Making API request..."
RESPONSE=$(curl -s -b "$COOKIES" \
  -X POST \
  -H "Content-Type: application/json" \
  -H "X-CSRF-Token: $CSRF_TOKEN" \
  -d '{"entity_uuid":"a1b2c3d4-e5f6-7890-abcd-ef1234567890"}' \
  "$BASE_URL/my-module-api/join")

echo "Response: $RESPONSE"

# Step 4: Logout (optional)
curl -s -b "$COOKIES" \
  -X POST \
  "$BASE_URL/user/logout?_format=json" > /dev/null

# Clean up
rm -f "$COOKIES"

Debugging Tips

View Response Headers

curl -i -b /tmp/cookies.txt \
  https://<project>.ddev.site/api/endpoint

Verbose Output

curl -v -b /tmp/cookies.txt \
  https://<project>.ddev.site/api/endpoint

Check Cookie Contents

cat /tmp/cookies.txt

Test if Authenticated

# Should return current user info if authenticated
curl -b /tmp/cookies.txt \
  https://<project>.ddev.site/user/login_status?_format=json

Security Notes

  • Never commit cookies or credentials to version control
  • Use environment variables for sensitive data in scripts
  • Session cookies expire after inactivity
  • HTTPS should always be used in production
  • Cookie files should be stored securely with appropriate permissions (chmod 600)

Common Response Codes

  • 200 OK - Request successful
  • 201 Created - Resource created successfully
  • 204 No Content - Successful request with no response body
  • 400 Bad Request - Invalid request payload
  • 401 Unauthorized - Not authenticated
  • 403 Forbidden - Authenticated but lacking permissions
  • 404 Not Found - Resource doesn't exist
  • 422 Unprocessable Entity - Validation errors
  • 500 Internal Server Error - Server-side error

See Also

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts1mo3 checks · Risk SAFE
  • Gen Agent Trust Hub1mo

    The skill provides patterns and templates for creating Drupal API endpoints using best practices like UUIDs, access controls, and CSRF protection. It includes debugging commands for local development environments.

  • Socket1mo

    No alerts

  • Snyk1mo

    Risk: LOW · No issues

Signed by skilld at 19514f4. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 2 months ago

README badge

README badge for grasmash/drupal-claude-skills/drupal-api-endpoints