The raw Response returned by fetch() can be accessed through the .asResponse() method on the APIPromise type that all methods return.
You can also use the .withResponse() method to get the raw Response along with the parsed data.
Using .asResponse()
The .asResponse() method returns the raw Response object without parsing the body:
import Cloudflare from 'cloudflare';
const client = new Cloudflare();
const response = await client.zones
.create({ account: { id: '023e105f4ecef8ad9ca31a8372d0c353' }, name: 'example.com', type: 'full' })
.asResponse();
console.log(response.headers.get('X-My-Header'));
console.log(response.statusText);
Use .asResponse() when you need to access headers, status codes, or other response metadata.
Using .withResponse()
The .withResponse() method returns both the parsed data and the raw Response object:
import Cloudflare from 'cloudflare';
const client = new Cloudflare();
const { data: zone, response: raw } = await client.zones
.create({ account: { id: '023e105f4ecef8ad9ca31a8372d0c353' }, name: 'example.com', type: 'full' })
.withResponse();
console.log(raw.headers.get('X-My-Header'));
console.log(zone.id);
.withResponse() is useful when you need both the parsed data and response metadata.
Common use cases
Response headers contain useful metadata like rate limits and request IDs:
const { data: zones, response } = await client.zones.list().withResponse();
console.log('Rate limit remaining:', response.headers.get('x-ratelimit-remaining'));
console.log('Request ID:', response.headers.get('cf-ray'));
console.log('Content type:', response.headers.get('content-type'));
Checking status codes
Access the HTTP status code and status text:
const response = await client.zones
.get({ zone_id: '023e105f4ecef8ad9ca31a8372d0c353' })
.asResponse();
console.log('Status:', response.status);
console.log('Status text:', response.statusText);
console.log('OK:', response.ok);
Inspecting response timing
You can measure request duration by tracking timestamps:
const startTime = Date.now();
const response = await client.zones.list().asResponse();
const duration = Date.now() - startTime;
console.log(`Request took ${duration}ms`);
Common Cloudflare API response headers:
Unique request identifier for debugging
Number of requests remaining in the current rate limit window
Unix timestamp when the rate limit window resets
Total number of requests allowed in the rate limit window
Access pagination metadata from response headers:
const { data: accounts, response } = await client.accounts
.list()
.withResponse();
// Check if there are more pages
const hasMore = response.headers.get('x-pagination-has-more');
const cursor = response.headers.get('x-pagination-cursor');
console.log('Has more pages:', hasMore);
console.log('Next cursor:', cursor);
Pagination header names may vary by endpoint. Check the specific API documentation for details.