Graftcode Context library

Graftcode Context provides a standardized way to access request context (headers and metadata) during Graftcode invocations. The library is available for multiple platforms and programming languages.

Overview

The RequestContext class provides a thread-safe (or context-aware) singleton that allows you to retrieve headers and custom metadata anywhere in your code during the request lifecycle.

Note

On the server side, headers are automatically set by Graftcode Gateway which hosts your code. On the client side (in Grafts), headers can be set using the GraftConfig class.

This is essential for accessing authentication tokens, correlation IDs, tenant information, and other request-scoped data propagated through Graftcode service calls.


Setting Headers in Grafts (Client Libraries)

Grafts are client libraries generated by Graftcode that allow you to call remote services. Unlike server-side code where headers are set automatically by Graftcode Gateway, in Grafts you need to set headers explicitly using the GraftConfig class.

The GraftConfig class is automatically generated as part of every Graft package by Graftcode Engine. It provides two methods for setting headers:

GraftConfig.setHeaders

Sets headers globally for all subsequent Graft invocations. Use this when you want to set headers once (e.g., at application startup or after authentication) and have them applied to all calls.

GraftConfig.invokeWithHeaders

Sets headers only for a specific function invocation. The headers are scoped to that single call and do not affect other invocations. Use this when you need different headers for different calls or want to temporarily override global headers.

Examples by Technology

import { GraftConfig, MyService } from '@graft/nuget-MyService';
// Set headers globally
GraftConfig.setHeaders({
'Authorization': 'Bearer token123',
'X-Correlation-Id': 'abc-123'
});
// Or set headers for a specific invocation
const result = GraftConfig.invokeWithHeaders(
() => MyService.doSomething(),
{ 'Authorization': 'Bearer different-token' }
);
---

Available Libraries

TechnologyPackage NamePackage URL
Node.jsgraftcode-contextnpmjs.com/package/graftcode-context
.NETGraftcode.Contextnuget.org/packages/Graftcode.Context
Javacom.graftcode:graftcode-contextcentral.sonatype.com/artifact/com.graftcode/graftcode-context
Pythongraftcode-contextpypi.org/project/graftcode-context
PHPgraftcode/graftcode-contextpackagist.org/packages/graftcode/graftcode-context
Rubygraftcode-contextrubygems.org/gems/graftcode-context

Installation

Install the library for your Receiver runtime from the public package registry:

npm install graftcode-context
### Requirements
TechnologyPackageMinimum runtime
Node.js / TypeScriptgraftcode-contextNode.js >= 22.0.0
.NETGraftcode.Context.NET Standard 2.1 or .NET 8.0
Java / JVMcom.graftcode:graftcode-contextJava 8+
Pythongraftcode-contextPython >= 3.9
PHPgraftcode/graftcode-contextPHP >= 7.4 or >= 8.0
Rubygraftcode-contextRuby >= 3.1.0

Usage

Read headers and metadata from the current request context anywhere in your Receiver code during a Graftcode invocation:

import { RequestContext } from 'graftcode-context';
const headers = RequestContext.current.getHeaders();
const authToken = headers['Authorization'];
const correlationId = headers['X-Correlation-Id'];
const tenantId = headers['X-Tenant-Id'];
### Thread and async safety
TechnologyMechanism
.NET[ThreadStatic] on RequestContext.Current
Java / JVMThreadLocal
Pythoncontextvars.ContextVar (async-safe with asyncio)
Node.js, PHP, RubyContext-aware singleton for the active request