Cross Program Invocation (CPI) is a mechanism in Solana that allows one program to invoke another program's instructions on the runtime, enabling programs to perform operations they cannot do themselves, such as account creation via system programs; this is implemented using the 'invoke' keyword in native Rust or CPIContext in Anchor, where developers create instructions for their program and pass them to the target program without needing to write instructions for the target program on the client side.
Understanding Solana CPIs: Cross-Program Invocation Tutorial
Added:hello everybody today we're going to actually cover a pretty simple topic but it can be really confusing when you first encounter it so we're going to cover cross program invocations or cpis now all a CPI is is it's kind of exactly what it sounds like you're using one program to go invoke another program right so hence the cross program invocation term so what does this really mean well let's say your program is trying to do something that it really can't do right like it needs another program to conduct some operation what you can do is from your program you can create a new instruction and then go invoke another program on the runtime with that instruction and the nice thing is you only have to send one instruction or whatever kind of instructions you have set up for your program and you don't have to actually create the instructions for the other program on your client side so let's take a look at a CPI in code so here's a program where we're going to actually use a CPI to create an account which we've seen before from the client side right so here's going to be the accounts that we're going to need and then here right here with this invoke keyword is actually where we're going to do the CPI right so invoke is how you go invoke another program from your program and then this function here from system instruction create account you can see that this is really just going to return to us an instruction right so we use this function passing the pams and we get back an instruction and this instruction is set up exactly the way we would expect right so with this instruction we can actually send over that instruction to our other program and then right here is just a list of accounts that are going to be involved accounts from our program that we're going to pass to that program and then we just process the result so check this out guys this is our test file here and as I said we only need to create the instructions for our program right so this is one instruction it's targeting our program and nowhere in this test file do we have to create the instructions to send to the system program now if we check the logs while we run our test we can go ahead and see that right here is where we invoke our program right this is our program ID same as up here and then we have these logs that we created but then right here right when we say new public key will be blah blah blah you can see we're actually invoking the system program which makes sense right like that's the invoke keyword that's exactly what we're doing and then it completes its operation and then we return to our program and finish out the rest of the script so that's how you do it with Native right so invoke is all you need for Native and then we've got some functions in the rust program library that will help us build instructions that will'll send to different programs now with anchor this looks pretty similar right we also have those functions to help us create instructions but the only difference is Anor uses what's called CPI context right and that's really just because anchor has these context structs and they do a lot of like interesting magic behind the scenes with these things so naturally they set up context for cross program inv vacations as well right because this is how anchor programs receive accounts that's all that really means and then within CPI context you can set up a new one with the target program and then you can use these embedded structs to actually create the instructions that you need and so this is obviously going to be the exact same thing we just did it's going to be just creating a new account with the system program but there you have it guys cross program invocations not so scary after all in fact these are super powerful because in a lot of use cases maybe with ownership or some other kind of constraints your program can't do something these are exactly what you're going to need to Implement in order to get things done [Music] later
Up Next

Cross-Program Invocations in Solana: Building a Puppet Program
@GajeshSNaik
1.2K views•2022-01-27

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Operational Security Essentials: A Guide for Hacktivists (OPSEC)
@hitbsecconf
157.4K views•2012-11-26

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto





![How To Get Started on Solana [Tel Aviv Hacker House Talk] - Jun 26th '23](https://i.ytimg.com/vi/ygtSTydWV-g/maxresdefault.jpg)






![Anchor Interfaces: Supporting Both Token Programs [Solana Tutorial] - Aug 17th '24](https://i.ytimg.com/vi_webp/ew0nOQRwYuM/maxresdefault.webp)



![Fundermentals: Accounts, Instructions and Transactions [Solana Basics Day2] - Feb 7th '26](https://i.ytimg.com/vi_webp/fXKyOrudpgA/maxresdefault.webp)



![Advanced CPI calls in Anchor [Solana Tutorial] - May 9th '23](https://i.ytimg.com/vi_webp/AKOWg65QWbQ/maxresdefault.webp)














