Create a Custom Sparkling Method
A Sparkling Method is a typed JS ↔ native bridge package that works on both Android and iOS. You define the API in TypeScript, generate native code with the CLI, then implement business logic in Kotlin and Swift.
1. Create a method package
Use the sparkling-method-cli to scaffold a new method module:
This creates a sparkling-my-greeting/ directory:
The module.config.json defines module metadata used by both codegen and autolink:
2. Write TypeScript function declarations
Edit the .d.ts file under src/ to declare your method's API.
You only write function declarations and interfaces — no implementation.
Key rules for declarations:
- Use
declare functionsyntax (notexport function) - The last parameter can be a callback
(result: ResponseType) => void - Use
@defaultJSDoc tags for default values - Literal union types (e.g.
'formal' | 'casual') become enum constants in native code - Interfaces can be nested — they'll generate nested model classes
3. Run codegen
Generate all platform code from your declarations:
This reads all .d.ts files under src/ and generates:
Do NOT manually edit generated files. If you need to change the API,
update the .d.ts declarations and re-run codegen.
4. Implement native business logic
The generated native code contains abstract classes / stubs that handle parameter parsing and response serialization. You extend them with actual business logic.
Android (Kotlin)
Create a concrete implementation extending the generated abstract class:
iOS (Swift)
Create a concrete implementation extending the generated class:
5. Build and test with autolink
Build the package
Compile the TypeScript:
Integrate into your Sparkling app
In your Sparkling app project, install the method package:
Then run autolink to wire up the native dependencies:
Autolink automatically:
- Android: updates
settings.gradle.ktsandapp/build.gradle.ktsto include the method module, and generatesSparklingAutolink.ktregistry - iOS: updates the
Podfilewith the pod dependency, and generatesSparklingAutolink.swiftregistry
Run and test
Call the method from your Lynx/JS code:
6. Publish
When the method is ready for distribution:
- Update
package.jsonversion and metadata - Verify
filesfield includes all necessary artifacts:
- Publish to npm:
Consumers then install and autolink:
Best practices
- Naming convention: package name should follow
sparkling-<module>format. Method names use<module>.<action>(e.g.mygreeting.hello). - Single source of truth: always edit
.d.tsdeclarations and re-run codegen — never edit generated files directly. - Platform parity: implement the same behavior on both Android and iOS.
- Error handling: return
code: 0for success and non-zero codes for errors, with a descriptivemsg. - Testing: test on both platforms before publishing.

